This is based on my previous question.
I have code that is failing with borrow checker error and I reduced it as much as I could. This is the result:
struct MyStruct<'a> {
s: &'a str,
}
impl<'a> MyStruct<'a> {
fn foo(&'a mut self) {}
}
fn main() {
let mut m = MyStruct { s: "aaa" };
m.foo();
m.foo();
}
It is failing with:
error[E0499]: cannot borrow `m` as mutable more than once at a time
--> src/main-x.rs:13:5
|
12 | m.foo();
| - first mutable borrow occurs here
13 | m.foo();
| ^
| |
| second mutable borrow occurs here
| first borrow later used here
The code was reduced so much that it does nothing useful and could be fixed for example by removing the 'a
lifetime from the foo
function. But I would like to understand why is the code not ok as it is.
My understanding is that MyStruct
contains reference to str
of some lifetime 'a
and foo
can be called with self
pointing to MyStruct
of the same lifetime. I don't see why is m
considered mutably borrowed after the first call to foo
.