我的测试代码:
let mut c = 0;
let mut inc = || { c += 1; c };
drop(inc);
println!("{}", c);
rustc 说:
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
但是在借用inc
之前手动删除,不是吗?println!
c
那么我的代码有什么问题?请帮忙。