I'm working on a piece of code that has some structs which contain references to each other, and I've run into an issue that I can't seem to figure out.
// demonstration of what the problem is
struct State<T>(T);
struct Query<'a, T>(&'a T);
impl<T> State<T> {
fn query(&mut self) -> Option<Query<'_, T>> {
unimplemented!()
}
}
struct Container<T>(Vec<State<T>>);
impl<T> Container<T> {
fn query(&mut self) -> Option<Query<'_, T>> {
let query = self.0.last_mut()?.query();
if let Some(query) = query {
return Some(query);
}
// i feel like i should be able to use &mut self again here but i can't :(
self.0.pop();
// error[E0499]: cannot borrow `*self` as mutable more than once at a time
return self.query();
// error[E0499]: cannot borrow `*self` as mutable more than once at a time
}
}
Why, in this example, can I not borrow again after the if block? Why does the lifetime of the borrow continue on after returning?