Looking at the predicates like skip_while and take_while they're
skip_while<P>(self, predicate P) -> ...
where Self: Sized, P: FnMut(&Self::Item) -> bool;
If a predicate is just returning true or false, and not modifying the values, why does it have to be FnMut and not Fn (like Fn(&Self::Item) -> bool)?
The
MutinFnMutmeans that the predicate can have a mutable state. It says nothing about the parameters.Additionaly,
FnMutis a super-trait ofFn, so you can also pass anFnwhereFnMutis accepted.As a silly example, consider this closure that stores a state (hence is
FnMut, but notFn), but cannot modify the elements in the iterator:(Permalink to the playground)
Output: