我正在学习Rust
,我在try 举例时遇到了这样的情况:
fn main() {
let x: &str = "hello"; // We created a string slice, `&str`
let y: &str = x[2..4]; // I was expecting `y` to be `&str` as well
// let y: str = x[2..4]; // `y` is actually a str (DST)!
let z: &str = &x[2..4]; // This gives me what I expected!
println!("str value: {}", z);
}
首先,我相当困惑[编辑];x
不是已经是一小块了吗?为什么我需要在最后一条语句中再次附加与号,以使z成为字符串片(在脑海中它看起来像&(&x)
或&&x
)?