小编use*_*682的帖子

为什么 Option<String>.as_ref() 不会解引用到 Option<&str>?

我希望这两个代码示例的结果相同:

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)

string optional rust

6
推荐指数
1
解决办法
1126
查看次数

std::string addition to char*

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 …

c++ stdstring

2
推荐指数
1
解决办法
61
查看次数

标签 统计

c++ ×1

optional ×1

rust ×1

stdstring ×1

string ×1