tl;dr given pub fn func(&'a mut self)
, why is self
considered "mutably borrowed" after func
has run?
Given the following minimal viable example (playground)
pub struct Struct1<'a> {
var: &'a u8,
}
impl<'a> Struct1<'a> {
pub fn new() -> Struct1<'a> {
return Struct1 {
var: &33,
}
}
pub fn func(&'a mut self) -> () {
()
}
}
fn main() {
let mut s1 = Struct1::new();
s1.func(); // point 1
// point 2
s1.func(); // point 3
}
results in compiler error
error[E0499]: cannot borrow `s1` as mutable more than once at a time
--> src/test12-borrow-mut-struct-twice-okay.rs:20:5
|
18 | s1.func(); // point 1
| -- first mutable borrow occurs here
19 | // point 2
20 | s1.func(); // point 3
| ^^
| |
| second mutable borrow occurs here
| first borrow later used here
However, at // point 2
the s1
appears to me to not be anymore borrowed. The func
is done running. What could be still be borrowing self
within func
!? It appears func
at //point 1
has relinquished control of s1
.
What is still borrowing s1
at // point 3
?
Similar questions: