Iterators

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 build a "shuffle" iterator: returns the elements of a collection in shuffled order
Last modified: Thursday, 10 May 2018, 6:38 PM