get()
returns a reference to a vector item, pushing the value beforehand if necessary:
struct Container {
vec: Vec<i32>,
}
impl Container {
fn find(&mut self, v: i32) -> Option<&mut i32> {
None // we don't care the implementation
}
fn get(&mut self, v: i32) -> &mut i32 {
match self.find(v) {
Some(x) => x,
None => {
self.vec.push(v);
self.vec.last_mut().unwrap()
}
}
}
}
fn main() {
let mut container = Container { vec: Vec::new() };
let x = container.get(42);
}
It does not compile because self
is still borrowed when we try to push the value (self.vec.push(v)
):
error[E0499]: cannot borrow `self.vec` as mutable more than once at a time
--> test.rs:13:17
|
10 | match self.find(v) {
| ---- first mutable borrow occurs here
...
13 | self.vec.push(v);
| ^^^^^^^^ second mutable borrow occurs here
...
17 | }
| - first borrow ends here
This is due to non-lexical lifetime.
So as a workaround, I thought I could rewrite get()
to limit the scope of self.find()
:
fn get(&mut self, v: i32) -> &mut i32 {
if let Some(x) = self.find(v) {
return x;
}
self.vec.push(v);
self.vec.last_mut().unwrap()
}
Unfortunately, this does not work:
error[E0499]: cannot borrow `self.vec` as mutable more than once at a time
--> test.rs:13:9
|
10 | if let Some(x) = self.find(v) {
| ---- first mutable borrow occurs here
...
13 | self.vec.push(v);
| ^^^^^^^^ second mutable borrow occurs here
14 | self.vec.last_mut().unwrap()
15 | }
| - first borrow ends here
I can't understand why.