这个问题和代码改编自Why does Creating a mutable reference to a dereferenced mutable reference work? . 那里的答案解释了重新借用,但没有解释围绕它的约定的原因。
下面的两个调用test
似乎等效,为什么只有第一个有效?
fn main() {
let mut string = String::new();
let ref_string = &mut string;
// Compiles
test(&mut *ref_string);
// Doesn't compile
test(&mut string);
}
fn test(s: &mut String) {
s.push('t');
}