-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
At their core, iterators exist to make it easier to perform an operation or a series of operations on the items of a container. Behind this simple goal lies a lot of complexity. In this section, we'll examine what an iterator in Rust actually is, learn about the properties of iterators, and learn about ways to create them.
In Rust, an iterator is any structure that implements the Iterator trait from the std::iter module. The structure exists to hold the state required to produce the next value of the iteration. For instance, if we were iterating through the items of Vec or slice, the structure may store a reference to the slice and the index of the next element to visit.
The Iterator trait, on the other hand, exposes one required method that alone drives all iteration: the next() method. Iteration in Rust revolves around the simplicity of this one function, which returns an option containing the value of the next...