Iterators

  • Iterator is a trait, with an Item associated type

  • Produces a sequence of Option<Item> values on calls to its next() method

  • Ubiquitous in Rust code

Iterator Adapters

  • Implementation of Iterator() that operates by consuming the output of some passed iterator; essentially iterators over iterators

  • Ubiquitous in Rust code

  • Book pp. 329-351 are a bestiary of Standard Library stuff, worth studying in detail

FromIterator

  • Trait used by collect() to make an iterator into a collection

  • Can implement for your collection type to make it collectible

IntoIterator

  • Trait for getting a "canonical" iterator for a type

  • Makes the type work with for loops

      trait IntoIterator where Self::IntoIter::Item == Self::Item {
          type Item;
          type IntoIter: Iterator;
          fn into_iter(self) -> Self::IntoIter;
      }
    
  • Every Iterator has a default implementation of IntoIterator, so that you can for loop over an explicit iterator

Implementing an Iterator

  • Let's look at a "Neighborhood" iterator, which examines the squares around a target square in a grid. (playground)
Last modified: Monday, 22 July 2019, 3:27 AM