Open
Description
See the following code:
use std::collections::HashMap;
struct Test {
v: u32,
}
fn main() {
let mut map = HashMap::new();
map.insert("a", Test { v: 0 });
let values = map.values();
for mut t in values {
t.v += 1;
}
}
It raises this compiler error:
error[E0594]: cannot assign to field
t.v
of immutable binding
--> /tmp/test.rs:14:9
|
14 | t.v += 1;
| ^^^^^^^^ cannot mutably borrow field of immutable binding
The fix is to use values_mut()
instead of values()
.
But the error message is confusing as the problem here is not the binding as t
itself is mut
making it harder to debug for newcomers.