这是我的问题:
fn main() {
let mut s = String::from("hello");
let s1 = &mut s;
let s2 = s1;
*s2 = String::from("world1");
*s1 = String::from("world2");
println!("{:?}", s);
}
Run Code Online (Sandbox Code Playgroud)
这将导致编译错误,因为 s1 具有&mut String未实现Copy特征的类型。
但是,如果我更改代码如下:
fn c(s: &mut String) -> &mut String {
s
}
fn main() {
let mut s = String::from("hello");
let s1 = &mut s;
let s2 = c(s1);
*s2 = String::from("world1");
*s1 = String::from("world2");
println!("{:?}", s);
}
Run Code Online (Sandbox Code Playgroud)
它将编译而没有任何错误消息。
我知道当一个引用传递给一个函数时,它意味着引用borrows该值而不是拥有它。
但是在上面的情况下,似乎当 s1 被传递给fn c并立即返回时, s2 borroweds1 所以 s1 不能被取消引用,直到 s2 超出其生命周期范围。
那么当 s1 传入 时发生了fn c什么?
从 @Denys S\xc3\xa9guret 的提示来看,我猜当 s1 被传递给 时fn C,Rust core 将参数编译s1为类似的东西&mut *s1,因此存在 s1 的不可变借用。
这就是为什么如果我们把
\n*s2 = String::from("world1");\nRun Code Online (Sandbox Code Playgroud)\n在后面
\n*s1 = String::from("world2");\nRun Code Online (Sandbox Code Playgroud)\nRust 会告诉我们:
\nassignment to borrowed `*s1`\nRun Code Online (Sandbox Code Playgroud)\n当 s2 超出其生命周期范围时,就不再借用 s1,因此 s1 可以再次取消引用。
\n但我不太确定这是否是正确的解释。
\n