An iterator has an iteration state. It must know what will be the next element to give you.
So a vector by itself isn't an iterator, and the distinction is important. You can have two iterators over the same vector, for example, each with its specific iteration state.
But a vector can provide you an iterator, that's why it implements IntoIterator
, which lets you write this:
let v = vec![1, 4];
for a in v {
dbg!(a);
}
Many functions take an IntoIterator
when an iterator is needed, and that's the case for zip
, which is why
let rx = xs.iter().zip(ys.iter());
can be replaced with
let rx = xs.iter().zip(ys);