January 2019
Beginner to intermediate
554 pages
13h 31m
English
When the compiler figures out that a closure mutates a value referenced from the environment, the closure implements the FnMut trait. Adapting the same code as before, we have the following:
// fn_mut_closure.rsfn main() { let mut a = String::from("Hey!"); let fn_mut_closure = || { a.push_str("Alice"); }; fn_mut_closure(); println!("Main says: {}", a);}
The previous closure adds the "Alice" string to a. fn_mut_closure mutates its environment.
Read now
Unlock full access