I have a struct and a vector to hold instances of that struct. When I add an instance to the struct and then access it with last and try to change the value of a field, I get an error.
struct Foo {
bar: i32,
}
fn main() {
let mut foos = Vec::new();
let f = Foo{ bar: 10 };
foos.push(f);
let mut ff = foos.last().unwrap();
ff.bar = 1;
}
src/main.rs:14:5: 14:15 error: cannot assign to immutable field `ff.bar`
src/main.rs:14 ff.bar = 1;
What confuses me is that both the vector and the item returned by last are mutable. I've read the field level mutability page in the documentation, but that says the following:
The mutability of a struct is in its binding
In the code above, my bindings are all mut. What am I missing?
That's slightly simplified. Immutable references breaks that rule, and that's the problem in this case. .last() will return an immutable reference to the last element, meaning that you can't change it unless it's internally mutable, but that's an other story. What you want is .last_mut().
Is that why, if I try to get the length of the vector (when using last_mut), that it fails to compile? I'm not just borrowing the item returned but the entire vector?