Open
Description
I tried this code:
#[derive(Default)]
struct Foo { i: i32, j: i32 }
impl Foo {
fn modify_and_borrow(&mut self) -> &i32 { self.j += 1; &self.i }
}
fn main() {
let mut foo = Foo::default();
let mut refs = Vec::new();
for _ in 0..2 {
refs.push(foo.modify_and_borrow());
}
}
I expected to see this happen:
The error should say something like, "cannot borrow foo
as mutable while it is already borrowed as immutable."
Instead, this happened:
error[E0499]: cannot borrow `foo` as mutable more than once at a time
--> src/main.rs:12:19
|
12 | refs.push(foo.modify_and_borrow());
| ^^^ mutable borrow starts here in previous iteration of loop
Meta
Playground link: