The signature of Iterator::filter
is
fn filter<P>(self, predicate: P) -> Filter<Self, P>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
Because it has self
as the first parameter, I was assuming I would have to pass the iterator by value, thus moving ownership to that function. However,
I am able to call it with just a ref mut and the code compiles and runs. For example:
fn char_count(i: &mut impl Iterator<Item=char>, c: char) -> usize {
i.filter(|&x| c == x).count()
}
What am I missing here?