The following example does not compile as long as 'a
is used in &'a mut self
:
struct Foo<'a> {
a: &'a u64,
}
impl<'a> Foo<'a> {
fn mutate_internal(&'a mut self) {}
fn mutate(&'a mut self) {
self.mutate_internal();
self.mutate_internal(); // <- This call fails the borrow-check
}
}
The compiler surprised me with the following error message:
tests/lang.rs:1116:13: 1116:17 error: cannot borrow `*self` as mutable more than once at a time
tests/lang.rs:1116 self.mutate_internal();
^~~~
tests/lang.rs:1115:13: 1115:17 note: previous borrow of `*self` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*self` until the borrow ends
tests/lang.rs:1115 self.mutate_internal();
^~~~
tests/lang.rs:1117:10: 1117:10 note: previous borrow ends here
tests/lang.rs:1114 fn mutate(&'a mut self) {
tests/lang.rs:1115 self.mutate_internal();
tests/lang.rs:1116 self.mutate_internal();
tests/lang.rs:1117 }
^
Can you explain why that is ? Please note that the issue goes away if &'a mut self
becomes &mut self
.
Meta
✗ rustc --version
rustc 1.0.0-nightly (e2fa53e59 2015-03-20) (built 2015-03-20)