Rust Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Implementing custom iterators

The true power of a great language is the way in which it lets the programmer integrate with types in the standard library and around the general ecosystem. One way to do this is the iterator pattern: defined by the Gang of Four in their book Design Patterns (Addison-Wesley Professional, 1994), an iterator is an encapsulation of a pointer moving through a collection. Rust provides a range of implementations on top of the Iterator trait. Let's see how we can leverage that power with only a few lines of code.