Abstractions in Practice · 18 min · free lesson

Closure Capture Modes

Infer shared, mutable, or moved captures.

Core idea

A closure borrows, mutably borrows, or moves each captured value according to its body.

Fn reads captured data, FnMut changes it, and FnOnce may consume it.
How a closure uses its captures determines which call traits it supports.

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.

Practise this code in Playground ↗

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
  1. Capture ownership
  2. Return type
  3. 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.

  1. Separate capture from call: move transfers name into announce, so the closure owns the String. The body only borrows that capture to print it.
  2. Predict before checking: Call announce() twice and predict whether it compiles. Predict again with drop(name); added to the body, and for println!("{name}"); after the closure.
  3. Compare with evidence: Two calls compile as written. With drop(name); the second call gets E0382. Printing name after the closure gets E0382 either way, because move took 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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.