有什么用途:
static_castdynamic_castconst_castreinterpret_cast(type)valuetype(value)如何决定在哪些特定情况下使用哪个?
我有unsigned char*,想把它转换成std::string.你能告诉我最安全的方法吗?
将c样式字符串转换为std :: string的正确/最佳/最简单方法是什么?
转换应接受max_length,并在第一个\ 0 char处终止字符串,如果这发生在max_length charter之前.
此问题适用于Rust的预发布版本.
这个年轻的问题很相似.
我试图按io::println功能打印一个符号
fn main() {
io::println('c');
}
Run Code Online (Sandbox Code Playgroud)
但我得到了下一个错误:
$ rustc pdst.rs
pdst.rs:2:16: 2:19 error: mismatched types: expected `&str` but found `char` (expected &str but found char)
pdst.rs:2 io::println('c');
^~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
如何将char转换为字符串?
UPDATE
直接类型转换不起作用:
let text:str = 'c';
let text:&str = 'c';
let text:@str = 'c';
let text:~str = 'c';
Run Code Online (Sandbox Code Playgroud)
它返回:
pdst.rs:7:13: 7:16 error: bare `str` is not a type
pdst.rs:7 let text:str = 'c';
^~~
pdst.rs:7:19: 7:22 error: mismatched types: expected …Run Code Online (Sandbox Code Playgroud)