
Understand it
A closure borrows, mutably borrows, or moves each captured value according to its body. The move keyword transfers captures even when the body would otherwise only borrow them.
The callable traits describe how a closure uses its captured environment. A closure that only reads captures can be called through Fn. One that mutates a captured variable needs FnMut, so the closure value must be mutable when invoked. One that consumes a captured value may be callable only once and satisfies FnOnce. Fn is the most reusable capability; FnOnce is the least demanding bound for a caller that invokes a callback exactly once.
In the sample, move transfers name into announce, allowing the closure to own the string independently of the original binding. But the body merely prints by borrowing its captured string, so the closure can still be called more than once. Add drop(name) inside the body and it consumes the capture; a second call then fails. This is why move and FnOnce should not be treated as synonyms. Capturing by value answers who owns the data; the body determines what kind of calls remain possible.
Experiment with three closures: read a counter, increment it, and consume a string. Try passing each to functions bounded by Fn, FnMut, and FnOnce, then read the errors. A callback API should ask for no more capability than it needs. If it calls the callback once, an FnOnce bound accepts the widest set of closures. If it may call repeatedly without mutable access to the closure, Fn is the right promise.
Read the code
let name = String::from("Ferris");
let announce = move || {
println!("{name}");
};
announce();The example makes the contract visible in code.
Try it
Say who owns name after move, and how many times announce can be called.
What success looks like: You can explain why move alone does not make a closure FnOnce.
Check your understanding
What does move change about the captures?
Reveal the answer
- Capture ownership
- Return type
- Call syntax
Correct: Capture ownership.
Go deeper
Common misconception: “move makes a closure FnOnce, so announce can run only once.”
move only decides who owns name. announce prints by borrowing its capture, so it can be called repeatedly; adding drop(name) to the body is what makes it FnOnce.
- Separate capture from call:
movetransfersnameintoannounce, so the closure owns theString. The body only borrows that capture to print it. - Predict before checking: Call
announce()twice and predict whether it compiles. Predict again withdrop(name);added to the body, and forprintln!("{name}");after the closure. - Compare with evidence: Two calls compile as written. With
drop(name);the second call gets E0382. Printingnameafter the closure gets E0382 either way, becausemovetook it.
Transfer challenge: Write three closures: one reads a counter, one increments it, and one drops a String. Bind each with let, pass it to functions bounded by Fn, FnMut, and FnOnce, and read the errors.
Connections
- Iterator Pipelines: Transform lazily, then choose one consumer
- Phantom Types: Track a compile-time state with no stored value
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: Closure Capture Modes (courseId "ownership", lessonId "closure-capture-modes") Link: https://crabrix.com/learn/ownership/closure-capture-modes/ Goal: A closure borrows, mutably borrows, or moves each captured value according to its body. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/closure-capture-modes) 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.