Iterating Over a Vec

To build up our practice with Iterator, and to build some understanding for the next section on IntoIterator, let’s write an Iterator for a Vec. We’re going to need a struct that holds onto two values:

  • The Vec we’re iterating over

  • The next position in the Vec we want to look at

If you remember from covering Vecs and slices, the data type we use for indexing in a Vec is a usize (machine sized unsigned integer). So, we can write our VecIter as:

Get hands-on with 1200+ tech skills courses.