logo

赋值

Last Updated: 2023-09-23

放弃?

fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    println!("{} {}", s1, s2);
}
error[E0382]: borrow of moved value: `s1`

进阶!

Rust 中s2 = s1进行的是 move 的操作。如果想要 s1 和 s2 都有效,应该使用.clone()

let s1 = String::from("hello");
let s2 = s1.clone();
println!("{} {}", s1, s2);
// hello hello

在现代 C++中有类似的功能,std::move().