【发布时间】:2020-11-07 15:36:26
【问题描述】:
fn main() {
let mut name = String::from("Charlie");
let x = &mut name;
let y = x; // x has been moved
say_hello(y);
say_hello(y); // but y has not been moved, it is still usable
change_string(y);
change_string(y);
}
fn say_hello(s: &str) {
println!("Hello {}", s);
}
fn change_string(s: &mut String) {
s.push_str(" Brown");
}
当我将x 分配给y 时,x 已被移动。但是,当我在函数中使用具有移动语义的东西时,我希望它会被移动。但是,我仍然可以在后续调用后使用该引用。也许这与 say_hello() 采用不可变引用但 change_string() 采用可变引用但引用仍未移动有关。
【问题讨论】:
标签: rust borrow-checker ownership