Closures in Rust

A “regular” closure within Rust uses the following syntax

let name = String::from("PutridParrot");
let hello = || println!("Hello {}", name);

In this simple example, the name is captured within the closure, which is the function

|| println!("Hello {}", name);

The name variable remains usable after the closure, however there’s another type of closure is the Moving Closure which uses the move keyword i.e.

let name = String::from("PutridParrot");
let hello = move || println!("Hello {}", name);

The difference here is the the name variable is no longer usable after the closure. Essentially the closure takes ownership of all enclosed variables.

The main use of move closures is within threading, so the thread takes ownership of it’s data. Async blocks often require owned values. Passing values into boxed trait objects.