snippetrustCritical
When does a closure implement Fn, FnMut and FnOnce?
Viewed 0 times
andfnoncedoeswhenclosureimplementfnmut
Problem
What are the specific conditions for a closure to implement the
That is:
For instance, mutating the state of the closure on it's body makes the compiler not implement
Fn, FnMut and FnOnce traits?That is:
- When does a closure not implement the
FnOncetrait?
- When does a closure not implement the
FnMuttrait?
- When does a closure not implement the
Fntrait?
For instance, mutating the state of the closure on it's body makes the compiler not implement
Fn on it.Solution
The traits each represent more and more restrictive properties about closures/functions, indicated by the signatures of their
A closure
These restrictions follow directly from the type of
For information on closures, see Closures: Anonymous Functions that Can Capture Their Environment in The Rust Programming Language.
call_... method, and particularly the type of self:FnOnce(self) are functions that can be called once
FnMut(&mut self) are functions that can be called if they have&mutaccess to their environment
Fn(&self) are functions that can be called if they only have&access to their environment
A closure
|...| ... will automatically implement as many of those as it can.- All closures implement
FnOnce: a closure that can't be called once doesn't deserve the name. Note that if a closure only implementsFnOnce, it can be called only once.
- Closures that don't move out of their captures implement
FnMut, allowing them to be called more than once (if there is unaliased access to the function object).
- Closures that don't need unique/mutable access to their captures implement
Fn, allowing them to be called essentially everywhere.
These restrictions follow directly from the type of
self and the "desugaring" of closures into structs; described in my blog post Finding Closure in Rust.For information on closures, see Closures: Anonymous Functions that Can Capture Their Environment in The Rust Programming Language.
Context
Stack Overflow Q#30177395, score: 254
Revisions (0)
No revisions yet.