TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
FnMut trait in Rust represents a closure that can be called multiple times and may mutate its internal state. Rather than dictating how variables are captured from the environment, FnMut specifies the calling convention: it requires exclusive, mutable access (&mut self) to the closure’s environment during execution. A closure implements FnMut if its body requires mutable access to the captured environment, regardless of whether the variables are captured by mutable reference, captured by value (e.g., using the move keyword), or if the closure simply implements the more restrictive Fn trait (which automatically satisfies FnMut bounds).
Trait Signature
TheFnMut trait is defined in the standard library as a subtrait of FnOnce:
call_mut method takes &mut self, the closure instance itself must be bound as mutable (let mut) to be executed. The &mut self signature guarantees that the closure cannot be invoked concurrently, preventing data races on the captured state.
Syntax and Mechanics
When a closure mutates a variable from its environment, the compiler infers theFnMut trait.
Capture Modes
TheFnMut trait is orthogonal to the capture mode. A closure can implement FnMut while capturing data by value. If the move keyword is used, the closure takes ownership of the variables, but it still implements FnMut if it mutates that owned data internally:
Compiler Desugaring
To understand the memory layout and trait resolution, a closure capturing by mutable reference is conceptually desugared by the compiler into an anonymous struct and the corresponding trait implementations. BecauseFnOnce is a supertrait of FnMut, the compiler must implement both:
Borrowing Rules and Non-Lexical Lifetimes
When anFnMut closure captures variables by mutable reference (&mut T), it strictly enforces Rust’s borrowing rules regarding exclusive access. You cannot borrow the captured variables directly (neither mutably nor immutably) while the closure’s borrow is active.
However, due to Rust’s Non-Lexical Lifetimes (NLL), this exclusive mutable borrow lasts only until the closure’s last use in the control flow, not for the duration of its entire lexical scope. Once the closure is invoked for the final time and is no longer referenced, the borrow is released, and the original variables can be accessed again.
Trait Hierarchy
FnMut sits in the middle of Rust’s closure trait hierarchy: Fn -> FnMut -> FnOnce.
FnMutimpliesFnOnce: BecauseFnMutinherits fromFnOnce, any closure that implementsFnMutcan be passed to a function expecting anFnOnce. It is guaranteed to be callable at least once.FnimpliesFnMut: Any closure that implementsFn(requiring only&selfto execute) automatically satisfiesFnMutbounds. A function requiring anFnMutparameter will accept anFnclosure, as an immutable environment trivially satisfies a mutable calling convention that simply performs no mutations.
Master Rust with Deep Grasping Methodology!Learn More





