What are the specific conditions for a closure to implement the Fn
, FnMut
and FnOnce
traits?
That is:
FnOnce
trait?FnMut
trait?Fn
trait?For instance, mutating the state of the closure on it's body makes the compiler not implement Fn
on it.
Use FnOnce as a bound when you want to accept a parameter of function-like type and only need to call it once. If you need to call the parameter repeatedly, use FnMut as a bound; if you also need it to not mutate state, use Fn .
Rust's closures are anonymous functions you can save in a variable or pass as arguments to other functions. You can create the closure in one place and then call the closure elsewhere to evaluate it in a different context. Unlike functions, closures can capture values from the scope in which they're defined.
The traits each represent more and more restrictive properties about closures/functions, indicated by the signatures of their call_...
method, and particularly the type of self
:
FnOnce
(self
) are functions that can be called onceFnMut
(&mut self
) are functions that can be called if they have &mut
access to their environmentFn
(&self
) are functions that can be called if they only have &
access to their environmentA 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 implements FnOnce
, 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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With