Rust doesn’t actually use the word “lambda,” but it has closures, which are anonymous functions that you can create inline—conceptually the same thing.
Below is a tour of how they’re written, how they capture variables, and the way the compiler translates them under-the-hood.
1. Basic Syntax
// Regular function
fn add_one(x: i32) -> i32 { x + 1 }
// Closure (lambda)
let add_one_closure = |x: i32| -> i32 { x + 1 };
// Most of the time you omit types; the compiler infers them
let add_one_inferred = |x| x + 1;
println!("{}", add_one_inferred(41)); // 42
| … | introduces the parameter list, followed by an optional return type and a body.
2. Capturing the Environment
Closures automatically borrow variables they use from the surrounding scope.
let factor = 3;
let multiply = |x| x * factor; // `factor` is captured immutably (by shared reference)
println!("{}", multiply(5)); // 15
If the closure mutates the captured value, it needs a mutable borrow:
let mut count = 0;
let mut tick = |inc| { // `tick` itself must be `mut` to call it mutably
count += inc; // `count` is captured mutably (&mut)
count
};
println!("{}", tick(4)); // 4
println!("{}", tick(2)); // 6
If you move captured data into the closure (useful for threads or 'static lifetimes), use move:
let label = String::from("Job #42");
let job = move || println!("Running {label}"); // ownership of `label` moves in
// `label` is no longer accessible here
3. Closure Traits: Fn, FnMut, FnOnce
Behind the scenes every closure is an anonymous struct plus an impl of one of these traits:
| Trait | What it can do | Typical capture |
|---|
Fn | Call immutably (&self) | Shared borrow |
FnMut | Call mutably (&mut self) | Mutable borrow |
FnOnce | Call once, consumes itself (self) | Takes ownership (move) or needs drop |
The compiler picks the least-powerful trait that works.
Examples:
let read_only = |x| x + factor; // implements Fn
let mut accumulator = |inc| { // implements FnMut
count += inc;
count
};
let consumer = move |s: String| { // implements FnOnce
println!("consumed: {s}");
};
4. Passing and Returning Closures
Because each closure has its own anonymous type, you usually use a trait bound instead of the concrete type.
fn apply_twice<F>(mut f: F, x: i32) -> i32
where
F: FnMut(i32) -> i32,
{
f(x) + f(x)
}
let double = |n| n * 2;
println!("{}", apply_twice(double, 5)); // 20
Returning a closure needs either impl Trait (stable) or a boxed trait object:
fn make_adder(n: i32) -> impl Fn(i32) -> i32 {
move |x| x + n // `n` moved in; closure type is anonymous
}
// Dynamic dispatch (size erased)
fn make_dyn_adder(n: i32) -> Box<dyn Fn(i32) -> i32> {
Box::new(move |x| x + n)
}
5. How the Compiler Low-Keys Rewrites It
Consider:
let a = 10;
let closure = |b| a + b;
The compiler roughly expands it to something like:
// Imaginary code generated by the compiler
struct __Closure { a: i32 } // anonymous name
impl FnOnce<(i32,)> for __Closure {
type Output = i32;
extern "rust-call" fn call_once(self, args: (i32,)) -> i32 {
let b = args.0;
self.a + b
}
}
impl FnMut<(i32,)> for __Closure { /* delegating to call_once */ }
impl Fn<(i32,)> for __Closure { /* delegating to call_once */ }
let closure = __Closure { a }; // instance created
So a closure is literally just a struct holding the captured variables plus an impl of the appropriate Fn* trait(s).
6. Common Gotchas
- A closure that captures something by value becomes
FnOnce; you can only call it once unless it also implements Clone.
- Putting a closure in a data structure typically means boxing it:
Vec<Box<dyn Fn()>>.
- Lifetimes: a non-
move closure can’t outlive the data it borrows; use move or ensure the borrow lives long enough.
7. Tiny Cheat-Sheet
// Syntax
|x, y| x + y // expression body
|x: u32, y: u32| -> u32 { x + y } // annotated
// Capture
|x| println!("{x}") // &T
|mut x| { x += 1; x } // &mut T
move |v| println!("{v}") // T (by value)
// Traits
Fn // read-only
FnMut // may mutate captured state
FnOnce // may consume captured values
// Passing/Returning
fn foo<F: Fn(i32)>(f: F) { /* … */ } // generic
Box<dyn Fn(i32) -> i32> // trait object
TL;DR
• Closures in Rust are anonymous structs that implement Fn, FnMut, or FnOnce.
• They automatically capture any variables they use—by shared ref, mutable ref, or move.
• You interact with them through those traits, either generically (impl Fn(_)) or via trait objects (Box<dyn Fn>).
That’s the whole lambda story in Rust!