liveBook · Manning

FnMut

As explained in Learn Rust in a Month of Lunches

FnMut is a Rust callable trait for closures and function-like values that can be invoked through mutable access and may mutate their captured environment.

`FnMut` is a Rust trait for a callable value that can be invoked through mutable access. A closure that changes a variable it captured from its surroundings uses `FnMut`. It can be called more than once; the name does not mean “once.” section 11.6.3 section 12.1.1

fn main() {
    let mut my_string = String::from("I will be changed in the closure");
    let mut my_closure = || {
        my_string.push_str(" now");
        println!("{my_string}");
    };
    my_closure();
    my_closure();
}

The closure adds text to the captured string on each call. Both the string and the closure are declared `mut`, and the second call sees the first call’s change. section 11.6.3

Accepting an FnMut argument

A function can request a callable that it will invoke mutably: ```rust fn call_twice<F: FnMut()>(mut f: F) { f(); f(); } ``` The `FnMut()` bound says the callable takes no arguments and returns nothing. `mut f` lets the function call it mutably. A closure passed to `call_twice` can therefore update a captured value on each call. section 12.1.1

Choose a closure bound in a function signature according to what the callable must be allowed to do. `Fn` can be called without changing its captured environment; `FnMut` allows mutable access; `FnOnce` also allows a closure that consumes a captured value and cannot be called again. An `Fn` closure can satisfy an `FnMut` bound, and an `FnMut` closure can satisfy an `FnOnce` bound. section 11.6.3 section 12.1.2

`FnMut` also appears in iterator method signatures. In `F: FnMut(Self::Item) -> bool`, the callable receives an iterator item and returns a Boolean, as in `.all()`. The bound permits mutable invocation; it does not require the closure to change a captured variable. Functions can return a closure with a signature such as `impl FnMut(i32) -> i32`. section 12.1 section 12.2.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through mutable capture, repeated calls, generic bounds, and the closure-trait hierarchy without detailing closure implementation internals.
Examples
Uses a closure that extends a string twice, a two-call function, mutable city data, and returned number-changing closures.
What it assumes
Starts with closures that capture nearby variables and builds on basic familiarity with mutable variables and function signatures.
What you can do after
Build it from scratch: write an FnMut-bound function that calls a closure repeatedly or accepts changes to mutable data.
How it connects
Connects FnMut to closures, mutable references, generic bounds, iterator methods, Fn, FnOnce, and impl Trait.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.