April 2026
Intermediate
631 pages
16h 20m
English
With the iterator design pattern, different types can have a common interface for accessing the elements of these types sequentially. An iterator abstracts how iterations are implemented internally and how types are defined internally. Iterators are heavily used in Rust programs, and therefore, an important task is to understand how it works.
In the following sections, we’ll explain the basics of the Iterator and the related IntoIterator traits. We’ll also demonstrate how to iterate over collections.
The Rust standard library has a trait called Iterator, which any type can implement. The definition of this trait resembles the code shown in Listing 9.17.
trait Iterator { type Item; fn next(&mut self) ...
Read now
Unlock full access