我正在尝试编译以下代码,来自Rust官方网站上的Rust书 .
fn takes_slice(slice: &str) {
println!("Got: {}", slice);
}
fn main() {
let s = "Hello".to_string();
takes_slice(&s);
}
在编译时,它会抛出以下错误
/devl/rust/bc_09/src/main.rs:7:17:7:19错误:不匹配的类型:expected&str,found&collections :: string :: String(期望str,找到struct collections :: string :: String )/devl/rust/bc_09/src/main.rs:7 takes_slice(&s); ^〜错误:由于先前的错误而中止无法编译hello_world .
这是我正在运行的Rust版本:rustc 1.0.0-nightly(44a287e6e 2015-01-08 17:03:40 -0800)
1 回答
这是Rust术语中每晚的旧版本!足够大,以致
&String
- >&str
强制不可用 . 您只需要升级到更新的版本 .