[−]Keyword move
Capture a closure's environment by value.
move
converts any variables captured by reference or mutable reference
to owned by value variables. The three Fn
trait's mirror the ways to capture
variables, when move
is used, the closures is represented by the FnOnce
trait.
let capture = "hello"; let closure = move || { println!("rust says {}", capture); };Run
move
is often used when threads are involved.
let x = 5; std::thread::spawn(move || { println!("captured {} by value", x) }).join().unwrap(); // x is no longer availableRun
move
is also valid before an async block.
let capture = "hello"; let block = async move { println!("rust says {} from async block", capture); };Run
For more information on the move
keyword, see the closure's section
of the Rust book or the threads section