Here is an example from Rust by Example:
pub trait Iterator {
// The type being iterated over.
type Item;
// `any` takes `&mut self` meaning the caller may be borrowed
// and modified, but not consumed.
fn any<F>(&mut self, f: F) -> bool where
// `FnMut` meaning any captured variable may at most be
// modified, not consumed. `Self::Item` states it takes
// arguments to the closure by value.
F: FnMut(Self::Item) -> bool {}
}
Why bother using FnMut if the argument is taken by value, since the argument cannot be mutated anyways? In fact, why is FnMut even allowed here? It seems only FnOnce is permitted to do this:
It has been noted that Rust chooses how to capture variables on the fly without annotation. This is all very convenient in normal usage however when writing functions, this ambiguity is not allowed. The closure's complete type, including which capturing type, must be annotated. The manner of capture a closure uses is annotated as one of the following
traits:
Fn: takes captures by reference (&T)FnMut: takes captures by mutable reference (&mut T)FnOnce: takes captures by value (T)