Future and Callback Contracts · 14 min · free with an account

Fn, FnMut and FnOnce

Lesson preview · about 30%

The call trait follows what the closure does with captures, not just whether move appears.

Core idea

Move controls capture ownership; the closure body determines its call-trait capabilities.

Start with the idea

Fn closures can be called through a shared reference, FnMut permits a mutable receiver, and FnOnce consumes its receiver. The traits form a hierarchy: a closure that implements Fn also works where FnMut or FnOnce is required. Every closure supports at least FnOnce, but consuming a captured non-Copy value can make a closure callable only once.

The move keyword determines how captures enter the closure. It does not alone make the closure FnOnce-only. A move closure that merely reads owned data may still implement Fn. Conversely, mutating captured state generally requires FnMut. The example borrows a counter mutably, calls the closure twice, then ends its scope before reading the counter directly.

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 754 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep