You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![feature(overloaded_calls, unboxed_closures)]use std::mem;fnmain(){// error: cannot assign to immutable captured outer variable in a proc//let n = 0u8;letmut n = 0u8;letmut f = |&mut:| {
n += 1;println!("f.n = {}", n);};println!("Closure size: {}", mem::size_of_val(&f));f();f();f();println!("n = {}", n);}
Output
Closure size:1
f.n = 1
f.n = 2
f.n = 3
n = 0
Correct me if I'm wrong, but I think n shouldn't need to be mutable, because f captures (by value) a copy of n, and modifies its copy of n on each call. Whereas, the outer n remains unchanged.