Abstractions in Practice · 18 min · free with an account

Closure Capture Modes

Lesson preview · about 30%

Infer shared, mutable, or moved captures.

Core idea

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

Start with the idea

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.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Ownership Mastery