To clarify a bit more, iter
isn't a trait method -- it's just a conventional name for collections like Vec
. On those collections, it is usually equivalent to having something like all of this:
struct BorrowedIter<'a, T> { /* ... */ }
impl<'a, T> Iterator for BorrowedIter<'a, T> {
type Item = &'a T;
/* ... */
}
impl<T> Collection<T> {
fn iter(&self) -> BorrowedIter<'_, T> {
/* ... */
}
}
impl<'a, T> IntoIterator for &'a Collection<T> {
type Item = <Self::IntoIter as Iterator>::Item;
type IntoIter = BorrowedIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter() // Collection::<T>::iter(self)
}
}
But since your struct is an iterator, you don't need any of that; just use your self
.
If you're used to for
where you have to do something like...
// Works but then you realize you can't let it consume your vector
for value in my_vec { /* ... */ }
// Works but then you realize you need just the first three
for value in &my_vec { /* ... */ }
// Gives you some error about `Vec` not being an iterator
for value in my_vec.take(3) { /* ... */ }
// Works again
for value in my_vec.iter().take(3) { /* ... */ }
...and that is throwing you off, it may help to know that for
uses IntoIterator
, not Iterator
. That's why &my_vec
works, for example. But my_vec.take(3)
doesn't work because take
is an iterator method -- you have to create the iterator first before calling it -- and collections like Vec
are not themselves iterators. When you use a Vec
with for
like in the first line, it uses my_vec.into_iter()
to transform the Vec
into some kind of owning iterator -- a different type.
That's also why Vec
and other collections have the iter
method, to easily create a borrowing iterator. It's a lot nicer than writing this:
for value in (&my_vec).into_iter().take(3) { /* ... */ }
Every iterator also implements IntoIterator
-- the implementation simply returns the same object. But again, your struct is an iterator already, so you don't need to use .into_iter()
either. That is, if you were working with a for
loop, this would work (unlike the example above):
for value in self.take(3) { /* ... */ }