From the documentation:
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where P: FnMut(&Self::Item) -> bool
I don't see why it needs a mutable ref to self
. Can someone explain?
It needs to be able to mutate self
because it is advancing the iterator. Each time you call next
, the iterator is mutated:
fn next(&mut self) -> Option<Self::Item>;
Here is the implementation of find
:
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
for x in self.by_ref() {
if predicate(&x) { return Some(x) }
}
None
}
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