What is a Closure?
Closures are anonymous functions you can save in a variable or pass as arguments to other functions. Unlike normal functions, closures can capture values from the scope in which they are defined.
rust
The syntax uses vertical pipes
| | for parameters and optional curly braces for the body.Capturing Variables
Wait! Closures can capture variables from their environment in three ways, which map to the three
Fn traits:- Borrowing Immutablely: The closure uses a reference to the variable. (Trait:
Fn) - Borrowing Mutablely: The closure modifies the variable. (Trait:
FnMut) - Taking Ownership: The closure takes full control of the variable. (Trait:
FnOnce)
rust
The move Keyword
If you want to force a closure to take ownership of the values it uses (e.g., when passing a closure to a new thread), use the
move keyword.rust
Closures in Iterators
Closures are most commonly used with iterators to perform operations on collections.
rust
Summary
| Term | Syntax | Description |
|---|---|---|
| Closure | ` | x |
| Capture | Automatic | Using variables from the outer scope |
| move | `move | |
| Fn | Trait | Can be called multiple times, only borrows |
| FnMut | Trait | Can be called multiple times, mutates captures |
| FnOnce | Trait | Can be called exactly once, takes ownership |
| Inference | Type-safe | Compiler usually infers parameter types |
| Lazy | Deferred | Code only runs when the closure is CALLED |
| Best For | Iterators, thread spawning, callbacks | |
| Memory | Captured variables are stored in the closure's environment | |
| Sized | Closures have unique, unnameable types | |
| Polymorph | Use impl Fn or Box<dyn Fn> for function parameters | |
| Efficiency | Small closures are often inlined by the compiler | |
| Identity | Each closure has its own unique internal type |