Limit scope of borrow (automagically)?

What you want will be enabled by non-lexical lifetimes, whose stabilization is currently being worked on.

// The only addition:
#![feature(nll)]

pub fn main() {
    let mut i: u32 = 0;
    let mut f = || {
        i += 1;
    };
    f();
    println!("i = {}", i); 
}

Playground link

1 Like