Fn / FnMut / FnOnce
FnOnce, FnMut, and Fn are the three traits a callable can implement. They describe whether calling the value consumes it, mutates it, or only reads it.
Detailed Explanation
Calling a closure is a trait method. FnOnce::call_once takes self by value, so the closure can be called at most once if that is the only impl. FnMut::call_mut takes &mut self, so you can call it many times if you have a mutable binding. Fn::call takes &self, so you can call it through a shared reference. The compiler implements the ones your captures allow, and Fn implies FnMut implies FnOnce.
That hierarchy is why API authors pick the least demanding trait. If your function only needs to call the closure once, take F: FnOnce(). thread::spawn is FnOnce because the thread owns the closure and runs it once. If you call it in a loop, take F: FnMut(). Iterator::map is FnMut because each item calls the closure again. If you need to share the closure, take F: Fn().
A function item, fn add(a: i32, b: i32) -> i32, implements all three. A closure that captures nothing does too, and it can coerce to a function pointer fn(i32, i32) -> i32. A closure that moves a String out of itself in the body is only FnOnce. A closure that increments a captured counter is FnMut but not Fn.
FnOnce is not "this function is unsafe to call twice" in a documentation sense. It is "the type system will not let you call it twice" because the first call consumed it. If you need two calls, make the closure FnMut by not moving captures out, or clone the captures.
These traits are currently unstable to implement by hand for your own types. You write a closure or a function. The traits matter as bounds. Box<dyn Fn()> is a heap-allocated callable you can clone the box of (not the closure) and call through a shared reference. Box<dyn FnOnce()> requires FnBox-style calling by value; in modern Rust you call it as f() after the box is owned.
When you write FnOnce as a bound: one call, including thread spawn and and_then. When you write FnMut: iterator adapters and any loop. When you write Fn: shared callbacks, Arc<dyn Fn()>, re-entrant hooks.
Code Examples
fn call_once<F: FnOnce() -> i32>(f: F) -> i32 {
f()
}
fn call_mut<F: FnMut() -> i32>(mut f: F) -> i32 {
f() + f()
}
fn main() {
let name = String::from("ada");
let n = call_once(move || name.len()); // consumes name, FnOnce
println!("{n}"); // prints: 3
let mut n = 0;
let sum = call_mut(|| {
n += 1;
n
});
println!("{sum}"); // prints: 3 (1 + 2)
}Common Gotchas
An `FnOnce` closure cannot be called twice
The second call is a use-after-move. Remove the `move` out of a capture, or clone before wrapping.
Taking `Fn` when you only need `FnOnce` rejects valid closures
A closure that must move a `String` into a thread is `FnOnce`. A `Fn` bound will not accept it.
Function pointers are `fn`, the type
`fn()` is a pointer. `Fn()` is a trait. `fn()` implements `Fn`, `FnMut`, and `FnOnce`.
You cannot write a stable handwritten `impl Fn for MyType`
Use a closure, or store an `fn` pointer plus data and call it yourself.