Say I have a struct Inside:
struct Inside {
a: i32,
}
And another struct Test that holds Inside in it:
struct Test<'a>{
inside: &'a mut Inside,
}
In the implementation of Test, I would like to modify the value a:
impl<'a> Test<'a>{
fn helper(&mut self){
let check = self.inside; // fails; cannot move out of `self.inside` which is behind a mutable reference
}
}
Why does this fail? All I'm trying to do is get a reference to inside, so I can modify it. I can't directly change it via self.inside.a = 6 because I want to assign check to something else (it's like a linkedList and check is just a current node that keeps moving along the list, modifying its values.
The inexplicable thing is that forcing the type of check works:
impl <'a> Test<'a>{
fn helper(&mut self){
let check: &mut Inside = self.inside; //This works!
check.a = 6;
}
}
Unfortunately, my own use case is a little more complicated than this, and my a value is actually an option. Whenever I try to return a mutable reference of the value in a from helper(), I start to get lifetime errors (cannot infer an appropriate lifetime due to conflicting requirements at line &mut Inside)
But the question remains: Why does forcing the type of check work?