Situation:
I have a situation where I would like to call some method defined on the Iterator
trait on a function parameter. The function that I would like to call it is taking a parameter of a type which is a trait
called VecLike
. The function is called get_all_matching_rules
.
get_all_matching_rules
can receive either a Vec
or another similar home made type which also implements Iterator
. Of course both of these implement VecLike
. I was thinking of adding a function on VecLike
to have it return an Iterator
so that I could use it in get_all_matching_rules
.
If my parameter is named: matching_rules
I could then do matching_rules.iter().filter(
.
Question:
How do I return a non consuming iterator from a Vec
?
I'd like to be able to return a non consuming iterator on a Vec<T>
of type Iterator<T>
. I am not looking to iterate the items by calling .iter()
.
If I have (where self is a Vec):
fn iter<'a>(&'a self) -> Iterator<T> {
self.iter()
}
I get the following error:
error: mismatched types: expected `core::iter::Iterator<T>+'a`, found `core::slice::Items<'_,T>` (expected trait core::iter::Iterator, found struct core::slice::Items)
I would like to return the Iterator<t>
. If there is a better way to go at this rather than returning an Iterator
, I'm all ears.