How can it be that all closures implement "FnOnce"?

Viewed 98

I read chapter 13.01 of the Rust Book and there is this section that confuses me greatly. Excerpt (emphasis mine):

FnOnce consumes the variables it captures from its enclosing scope, known as the closure’s environment. To consume the captured variables, the closure must take ownership of these variables and move them into the closure when it is defined. The Once part of the name represents the fact that the closure can’t take ownership of the same variables more than once, so it can be called only once.

When you create a closure, Rust infers which trait to use based on how the closure uses the values from the environment. All closures implement FnOnce because they can all be called at least once. Closures that don’t move the captured variables also implement FnMut, and closures that don’t need mutable access to the captured variables also implement

Don't the highlighted sentences contradict one another? If Fn and FnMut closures can be called more than once, why do they inherit from FnOnce?

I understand how each trait works individually - it's this superposition of FnOnce plus {FnMut, Fn} that bugs me. Why haven't the FnMut and Fn traits been defined independently of FnOnce?

I read GitHub issue #1225 but that didn't clear up my confusion.

0 Answers
Related