Consider this code (playpen):
fn main() {
// types for clarity
let mut x: String = "hello".to_string();
x.push('x');
{
let y: &mut String = &mut x;
// y.push('x');
println!("{}", y);
}
{
// warning: variable does not need to be mutable
let mut z: &mut String = &mut x;
// z.push('x'); // this line disables the warning
println!("{}", z);
}
}
Mutability of the reference itself isn't needed for z.push('x'). Then the behavior in two nested blocks is inconsistent: mut in let mut z is, in fact, unneeded, either when doing z.push('x') or when not doing it. Only the underlying String has to be mutable. But the compiler doesn't warn about it.
I expected the warning "variable does not need to be mutable" to be emitted even when z.push('x'); line is uncommented.
Consider this code (playpen):
Mutability of the reference itself isn't needed for
z.push('x'). Then the behavior in two nested blocks is inconsistent:mutinlet mut zis, in fact, unneeded, either when doingz.push('x')or when not doing it. Only the underlyingStringhas to be mutable. But the compiler doesn't warn about it.I expected the warning "variable does not need to be mutable" to be emitted even when
z.push('x');line is uncommented.