
read only reads total, and bump mutates count.Understand it
Rust infers whether a closure needs a shared borrow (Fn), a mutable borrow (FnMut), or ownership (FnOnce) from what the body does. move forces capture by value, but sending a closure to another thread also requires Send and suitable lifetimes. Moving a reference does not extend the referent’s lifetime, and moving an Rc does not make it Send.
A closure is a callable value that may carry data from the surrounding scope. The first example only reads total, so it can satisfy Fn; the second changes count, so it needs mutable access and satisfies FnMut. A closure that consumes a captured value may be callable only once and satisfy FnOnce. These traits describe what callers may do with the closure, not just what the closure returns.
Capture is inferred per variable. A shared read usually borrows, a mutation needs an exclusive borrow, and consuming an owned value may move it into the closure. The move keyword forces capture by value, but it does not automatically make every call consume the captured value. A moved-in String can still be read on repeated calls if the body only borrows it. This distinction matters for thread work: unscoped thread::spawn requires a Send + 'static closure. Owned captures are one way to satisfy the lifetime bound; references to static data can also work. Scoped threads may borrow local data because they are joined before the scope exits.
Run the sample and try to read count while the mutating closure will be called again. The active mutable borrow explains the compiler's objection. Call the closure for the last time before reading the value, or confine it to a shorter scope. Then write a closure that calls drop(name) to see why it becomes FnOnce. Choose a callback bound based on how often the caller must invoke it and whether mutation is part of the contract.
A closure can capture different variables in different ways. One captured counter may be mutated while another captured configuration is only read. Reason about each capture separately instead of assigning one ownership mode to the whole surrounding scope.
All closures implement FnOnce. A closure that can be called through mutable access also implements FnMut; one callable through shared access also implements Fn. These bounds form a hierarchy, not three disjoint categories. Interior mutability can change data through a shared reference without requiring an FnMut receiver.
Read the code
let total = 10;
let read = || println!("total {total}"); // Fn
read();
let mut count = 0;
let mut bump = || count += 1; // FnMut
bump();
bump();
println!("count {count}");Two closures: read only reads total, and bump mutates count.
Practice starter
let name = String::from("ada");
let consume = move || name;Try it
Use a closure that captures minimum to keep strictly greater values in original order, including duplicates. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Which trait does that closure implement?
Reveal the answer
- Fn
- FnMut
- FnOnce
It gives the String away, so it can only be called once.
Go deeper
Common misconception: “move is what makes consume an FnOnce.”
The body decides. consume returns name, giving the String away, so it can run only once. A move closure that only reads name is still Fn.
- Classify each capture:
readonly readstotal,bumpchangescount, andconsumein the practice code givesnameaway. - Predict before checking: Add
println!("{count}");between the twobump();calls and predict the compiler's answer. Then predict whetherconsumecan be called twice. - Compare with evidence: Compile both: the print gets E0502 because
bumpstill holds its mutable borrow, and the secondconsume()gets E0382 because the first call used the closure up.
Transfer challenge: Remove move from consume and check that it still can be called only once. Then change the body to name.len(), put move back, and call it twice.
Connections
- Lifetimes: Relationships between borrows
- Iterators: Lazy transformations
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Closures (courseId "ownership", lessonId "closures") Link: https://crabrix.com/learn/ownership/closures/ Goal: Call traits depend on how the body uses captures: Fn borrows, FnMut may mutate, and moving a capture out can require FnOnce. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/closures) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.