If you look in the official Rust doc, you see that the trait Fn
is derived from FnMut
, or, to implement Fn
, you have to implement FnMut
(and after that FnOnce
since FnMut
also derives from it).
Why is that so? I simply can't comprehend that. Is it because you can call every Fn
as a FnOnce
or FnMut
?
The best reference for this is the excellent Finding Closure in Rust blog post. I'll quote the salient part:
There’s three traits, and so seven non-empty sets of traits that could possibly be implemented… but there’s actually only three interesting configurations:
Fn
,FnMut
andFnOnce
,FnMut
andFnOnce
,- only
FnOnce
.Why? Well, the three closure traits are actually three nested sets: every closure that implements
Fn
can also implementFnMut
(if&self works
,&mut self
also works; proof:&*self
), and similarly every closure implementingFnMut
can also implementFnOnce
. This hierarchy is enforced at the type level
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