What are the specific conditions for a closure to implement the Fn
, FnMut
and FnOnce
traits?
That is:
- When does a closure not implement the
FnOnce
trait? - When does a closure not implement the
FnMut
trait? - When does a closure not implement the
Fn
trait?
For instance, mutating the state of the closure on it's body makes the compiler not implement Fn
on it.
The traits each represent more and more restrictive properties about closures/functions, indicated by the signatures of their
call_...
method, and particularly the type ofself
:FnOnce
(self
) are functions that can be called once,FnMut
(&mut self
) are functions that can be called if they have&mut
access to their environmentFn
(&self
) are functions that can still be called if they only have&
access to their environment.A closure
|...| ...
will automatically implement as many of those as it can.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.FnMut
, allowing them to be called more than once (if there is unaliased access to the function object).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 Finding Closure in Rust).For information on closures in Rust as of 2017, see the Closures chapter in the Rust book.