Closures Basics
In this page:
Defining a Closure
A closure is written using pipes | | around its parameter list, followed by an expression or block. Closures can be stored in a variable and then called just like a function.
Example: Defining a Closure
fn main() {
let add_one = |x: i32| x + 1;
println!("{}", add_one(5));
}
Login to try C/C++/Java/PHP code in the editor
Capturing Variables from the Environment
Unlike regular functions, closures can directly use variables from the scope where they were defined, without needing to receive them as parameters.
Example: Capturing Variables from the Environment
fn main() {
let multiplier = 3;
let multiply = |x: i32| x * multiplier;
println!("{}", multiply(7));
}
Login to try C/C++/Java/PHP code in the editor
Type Inference in Closures
Closures typically don't need explicit parameter or return types annotated -- Rust infers them from how the closure is used, keeping closure syntax compact.
Example: Type Inference in Closures
fn main() {
let square = |x| x * x;
let result: i32 = square(6);
println!("{}", result);
}
Login to try C/C++/Java/PHP code in the editor
Multi-line Closure Bodies
When a closure needs more than one expression, its body can be a full block in curly braces, just like a function body.
Example: Multi-line Closure Bodies
fn main() {
let describe = |n: i32| {
let kind = if n % 2 == 0 { "even" } else { "odd" };
format!("{} is {}", n, kind)
};
println!("{}", describe(9));
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting closures can capture variables from their surrounding scope automatically, unlike regular
fnfunctions. - Writing unnecessary type annotations on every closure parameter, when Rust can usually infer them from usage.
- Confusing closure syntax
|x| x + 1with a bitwise OR expression when reading unfamiliar code.
- Closures are anonymous functions written with pipes around parameters, like
|x| x + 1. - Closures can capture variables from the environment they were defined in.
- Closure parameter and return types are usually inferred, unlike regular function definitions.
- Closures can be stored in variables and passed around like values.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: