这里没有什么奇怪的事情发生;每次
test_three
函数结束其工作(在调用之后):
fn main() {
let mut s = String::from("hello");
println!("{}", s); // immutably borrow s and release it
test_three(&mut s); // mutably borrow s and release it
println!("{}", s); // immutably borrow s and release it
test_three(&mut s); // mutably borrow s and release it
println!("{}", s); // immutably borrow s and release it
}
该函数不包含其参数-它只对
String
fn test_three(st: &mut String) { // st is a mutably borrowed String
st.push('f'); // the String is mutated
} // the borrow claimed by st is released