关于Rust的函数参数和所有权的一个小问题
这是我的问题:
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);
}
这将导致编译错误,因为 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);
}
它将编译而没有任何错误消息。
我知道当一个引用传递给一个函数时,它意味着引用borrows该值而不是拥有它。
但是在上面的情况下,似乎当 s1 被传递给fn c并立即返回时, s2 borroweds1 所以 s1 不能被取消引用,直到 s2 超出其生命周期范围。
那么当 s1 传入 时发生了fn c什么?