我希望这两个代码示例的结果相同:
let maybe_string = Some(String::from("foo"));
let string = if let Some(ref value) = maybe_string { value } else { "none" };
Run Code Online (Sandbox Code Playgroud)
let maybe_string = Some(String::from("foo"));
let string = maybe_string.as_ref().unwrap_or("none");
Run Code Online (Sandbox Code Playgroud)
第二个示例给了我一个错误:
let maybe_string = Some(String::from("foo"));
let string = if let Some(ref value) = maybe_string { value } else { "none" };
Run Code Online (Sandbox Code Playgroud) I'm trying to understand how addition of std::string to char* works. This code is compiling and working just as expected:
#include <string>
#include <cstdio>
void func (const char* str) {
printf("%s\n", str);
}
int main () {
char arr[] = {'a','b','c',0};
char *str = arr;
func((str + std::string("xyz")).c_str()); // THIS LINE
return 0;
}
Run Code Online (Sandbox Code Playgroud)
But I do not understand what constructors/methods is calling and in what order for this to work. This is addition of std::string to char* which gives …