I am having trouble with the following set up:
#[derive(Debug)]
struct SomeStruct {
numbers: Vec<u32>,
}
impl SomeStruct {
fn some_func(&mut self) { // `mut` causes the issue
self.numbers.push(9); // Contrived but need to call self in here.
println!("Hello from some func");
}
}
pub fn main() {
let struct1 = SomeStruct {
numbers: vec![1, 2, 3],
};
let struct2 = SomeStruct {
numbers: vec![99, 98, 97],
};
let mut vec = Vec::new();
vec.push(&struct1);
vec.push(&struct2);
let first = vec.first_mut().unwrap();
// cannot borrow `**first` as mutable, as it is behind a `&` reference
// cannot borrow as mutable rustc(E0596)
first.some_func();
}
There are many questions about mutable & borrowing but I still can't figure it out. Could someone please explain why this is wrong and how to fix it?