这个问题涉及到Rust的预发布版本。这个更年轻的问题类似于这个。
我尝试使用
println
打印一个符号:fn main() {
println!('c');
}
但是我遇到了以下错误:
$ 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 println!('c');
^~~
error: aborting due to previous error
如何将char转换为string?
直接强制类型转换不起作用:
let text:str = 'c';
let text:&str = 'c';
它返回:
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 `~str` but found `char` (expected ~str but found char)
pdst.rs:7 let text:str = 'c';
^~~
pdst.rs:8:20: 8:23 error: mismatched types: expected `&str` but found `char` (expected &str but found char)
pdst.rs:8 let text:&str = 'c';
^~~
ToString
特质,任何实现了Display
特质的内容都会自动实现该特质,包括char
。(impl<T> ToString for T where T: Display + ?Sized
) 然而,这种间接性意味着它不会在文档中显示,你需要自己记住或学习。 - Alex