Iterators

So far, we've assumed that the reader has been at least passingly familiar with Rust iterators. It's possible that you've used them extensively but have never written your own iterator implementation. That knowledge is important for what follows and we'll discuss Rust's iteration facility now.

A Rust iterator is any type that implements std::iter::Iterator. The Iterator trait has an interior Item type that allows the familiar iterator functions, such as next(&mut self) -> Option<Self::Item>, to be defined in terms of that generic Item. Many of the iterator functions are, well, functional in nature: map, filter, and fold are all higher-order functions on a stream of Item. Many of the iterator functions are searches on that stream of Item: max, min, position, and find. It is a versatile trait. If you find it limited in some manner, the community has put together a crate with more capability: itertools (https://crates.io/crates/itertools). Here, we're less concerned with the interface of Iterator than how to implement it for our own types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.197.95