My test code:
let mut c = 0;
let mut inc = || { c += 1; c };
drop(inc);
println!("{}", c);
rustc says:
error[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> .\src\closure.rs:20:24
|
12 | let mut inc = || { c += 1; c };
| -- ----- previous borrow occurs due to use of `c` in closure
| |
| mutable borrow occurs here
...
20 | println!("{}", c);
| ^^^^^ immutable borrow occurs here
21 | }
| - mutable borrow ends here
But inc
is dropped manually before println!
borrow c
, isn't it?
So what's the problem with my code? Please help.