capturing in closures in Rust '21

Viewed 192

I just found out that the following code compiles in Rust 21 (used to not compile in 18)

fn get_func (i: &mut i32) -> impl Fn() -> i32 + '_ {
    || *i
} 

Is there an implicit move of i involved ? If so, then why does the following code compile too?

fn get_func (i: &mut i32) -> impl Fn() -> i32 + '_ {
    let f = || *i;
    
    println!("{:?}", i);  // was expecting here to give borrow of moved variable error, `&mut` doesnt implement `Copy` trait
    
    f
}

Or instead is it implicitly moving (copying in this case) the value being pointed to? But then the following code should compile, which doesn't -- indicating it's moving the reference.

fn get_func (i: &mut i32) -> impl Fn() -> i32 {
    || *i
}
0 Answers
Related