Why does the following code fail in the borrow checker?
struct X(pub i32);
impl X {
fn run<F>(&mut self, f: F) -> i32
where
F: FnOnce() -> i32,
{
f() * self.0
}
}
struct Y {
a: i32,
b: X,
}
impl Y {
fn testme(&mut self) -> i32 {
let x = || self.a;
self.b.run(x)
}
}
fn main() {
let mut x = Y { a: 100, b: X(100) };
println!("Result: {}", x.testme());
}
With the error (Playground)
error[E0502]: cannot borrow `self.b` as mutable because `self` is also borrowed as immutable
--> src/main.rs:18:9
|
17 | let x = || self.a;
| -- ---- previous borrow occurs due to use of `self` in closure
| |
| immutable borrow occurs here
18 | self.b.run(x)
| ^^^^^^ mutable borrow occurs here
19 | }
| - immutable borrow ends here
It looks like the closure x
borrowed self
in testme()
; shouldn't it have only borrowed self.a
? Maybe I am missing something.