Fn, FnMut & FnOnce
Closures implement Fn (shared borrow), FnMut (mut borrow), or FnOnce (consume captures) based on usage.
Search across all documentation pages
Closures implement Fn (shared borrow), FnMut (mut borrow), or FnOnce (consume captures) based on usage.
fn call_twice(f: impl Fn() -> i32) -> i32 {
f() + f()
}
fn main() {
let n = 10;
println!("{}", call_twice(|| n));
}When to reach for this: Bounding closure parameters in generic APIs (thread::spawn needs FnOnce).
fn with_mut(f: impl FnMut(&mut Vec<i32>)) {
let mut v = vec![1];
f(&mut v);
}
fn main() {
with_mut(|v| v.push(2));
}What this demonstrates:
FnMut for mutating captured state or mut ref argsFnOnce required if closure consumes captured StringFn is subtrait of FnMut is subtrait of FnOnce. Prefer impl Fn when possible for flexibility.
FnOnce + Send + 'static - Captures must be owned. Fix: move closure.FnMut trait object - Box<dyn FnMut()>. Fix: Box or generic.FnMut.| Alternative | Use When | Don't Use When |
|---|---|---|
Function pointer fn() | No capture | Need environment |
Generic F: FnOnce | Flexible callback | Need homogenous vec of callbacks |
Box<dyn FnMut()> | Runtime storage | Monomorphization ok |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026