Even NLL is not so precise. The borrow still starts when the closure is created. The previous example has merely shown how NLL can reason about the “last use” of the mutable borrow.
#![feature(nll)]
pub fn main() {
let mut i: u32 = 0;
let mut f = || {
i += 1;
};
println!("i = {}", i);
f();
}
Compiling playground v0.0.1 (file:///playground)
error[E0502]: cannot borrow `i` as immutable because it is also borrowed as mutable
--> src/main.rs:8:24
|
5 | let mut f = || {
| -- mutable borrow occurs here
6 | i += 1;
| - previous borrow occurs due to use of `i` in closure
7 | };
8 | println!("i = {}", i);
| ^ immutable borrow occurs here
9 | f();
| - borrow later used here
That’s hard to say. One could look for the first nightly that added the feature gate, but chances are it doesn’t actually do anything in that version. It’s a feature under active development.