I have the following code:
let mut vec = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
vec.iter_mut().for_each(|(mut x, _y)| { x += 1 });
println!("{vec:?}");
// Just for reference: what the code originally set out to do, but missed because of copy and past from a line that included the `mut` above.
vec.iter_mut().for_each(|(x, _y)| { *x += 1 });
println!("{vec:?}");
Intuitively (at least as per my intuition...), the first iteration shouldn't compile: we are adding something to x
, but x
should be a mutable reference. And lo and behold, if I remove the mut
modifier in line 2 I get an error:
3 | vec.iter_mut().for_each(|(x, _y)| { x += 1 });
| -^^^^^
| |
| cannot use `+=` on type `&mut {integer}`
So...just adding the mut
keyword there apparently changes the type of x
from a mutable reference to an integer to a mere integer?
That seems like a rather large foot gun to me and I am struggling to classify it as anything but a bug. Is there some rationale to it or is it an actual issue?