2016-04-18 13:08:27 -05:00
|
|
|
//! Composable external iteration.
|
|
|
|
//!
|
|
|
|
//! If you've found yourself with a collection of some kind, and needed to
|
|
|
|
//! perform an operation on the elements of said collection, you'll quickly run
|
|
|
|
//! into 'iterators'. Iterators are heavily used in idiomatic Rust code, so
|
|
|
|
//! it's worth becoming familiar with them.
|
|
|
|
//!
|
|
|
|
//! Before explaining more, let's talk about how this module is structured:
|
|
|
|
//!
|
|
|
|
//! # Organization
|
|
|
|
//!
|
|
|
|
//! This module is largely organized by type:
|
|
|
|
//!
|
|
|
|
//! * [Traits] are the core portion: these traits define what kind of iterators
|
|
|
|
//! exist and what you can do with them. The methods of these traits are worth
|
|
|
|
//! putting some extra study time into.
|
|
|
|
//! * [Functions] provide some helpful ways to create some basic iterators.
|
|
|
|
//! * [Structs] are often the return types of the various methods on this
|
|
|
|
//! module's traits. You'll usually want to look at the method that creates
|
|
|
|
//! the `struct`, rather than the `struct` itself. For more detail about why,
|
|
|
|
//! see '[Implementing Iterator](#implementing-iterator)'.
|
|
|
|
//!
|
|
|
|
//! [Traits]: #traits
|
|
|
|
//! [Functions]: #functions
|
|
|
|
//! [Structs]: #structs
|
|
|
|
//!
|
|
|
|
//! That's it! Let's dig into iterators.
|
|
|
|
//!
|
|
|
|
//! # Iterator
|
|
|
|
//!
|
|
|
|
//! The heart and soul of this module is the [`Iterator`] trait. The core of
|
|
|
|
//! [`Iterator`] looks like this:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! trait Iterator {
|
|
|
|
//! type Item;
|
|
|
|
//! fn next(&mut self) -> Option<Self::Item>;
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! An iterator has a method, [`next`], which when called, returns an
|
|
|
|
//! [`Option`]`<Item>`. [`next`] will return `Some(Item)` as long as there
|
2016-04-18 13:08:27 -05:00
|
|
|
//! are elements, and once they've all been exhausted, will return `None` to
|
|
|
|
//! indicate that iteration is finished. Individual iterators may choose to
|
2017-03-12 13:04:52 -05:00
|
|
|
//! resume iteration, and so calling [`next`] again may or may not eventually
|
2016-04-18 13:08:27 -05:00
|
|
|
//! start returning `Some(Item)` again at some point.
|
|
|
|
//!
|
|
|
|
//! [`Iterator`]'s full definition includes a number of other methods as well,
|
2017-03-12 13:04:52 -05:00
|
|
|
//! but they are default methods, built on top of [`next`], and so you get
|
2016-04-18 13:08:27 -05:00
|
|
|
//! them for free.
|
|
|
|
//!
|
|
|
|
//! Iterators are also composable, and it's common to chain them together to do
|
|
|
|
//! more complex forms of processing. See the [Adapters](#adapters) section
|
|
|
|
//! below for more details.
|
|
|
|
//!
|
|
|
|
//! [`Iterator`]: trait.Iterator.html
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`next`]: trait.Iterator.html#tymethod.next
|
2016-04-18 13:08:27 -05:00
|
|
|
//! [`Option`]: ../../std/option/enum.Option.html
|
|
|
|
//!
|
|
|
|
//! # The three forms of iteration
|
|
|
|
//!
|
|
|
|
//! There are three common methods which can create iterators from a collection:
|
|
|
|
//!
|
|
|
|
//! * `iter()`, which iterates over `&T`.
|
|
|
|
//! * `iter_mut()`, which iterates over `&mut T`.
|
|
|
|
//! * `into_iter()`, which iterates over `T`.
|
|
|
|
//!
|
|
|
|
//! Various things in the standard library may implement one or more of the
|
|
|
|
//! three, where appropriate.
|
|
|
|
//!
|
|
|
|
//! # Implementing Iterator
|
|
|
|
//!
|
|
|
|
//! Creating an iterator of your own involves two steps: creating a `struct` to
|
|
|
|
//! hold the iterator's state, and then `impl`ementing [`Iterator`] for that
|
|
|
|
//! `struct`. This is why there are so many `struct`s in this module: there is
|
|
|
|
//! one for each iterator and iterator adapter.
|
|
|
|
//!
|
|
|
|
//! Let's make an iterator named `Counter` which counts from `1` to `5`:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! // First, the struct:
|
|
|
|
//!
|
|
|
|
//! /// An iterator which counts from one to five
|
|
|
|
//! struct Counter {
|
|
|
|
//! count: usize,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // we want our count to start at one, so let's add a new() method to help.
|
|
|
|
//! // This isn't strictly necessary, but is convenient. Note that we start
|
|
|
|
//! // `count` at zero, we'll see why in `next()`'s implementation below.
|
|
|
|
//! impl Counter {
|
|
|
|
//! fn new() -> Counter {
|
|
|
|
//! Counter { count: 0 }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // Then, we implement `Iterator` for our `Counter`:
|
|
|
|
//!
|
|
|
|
//! impl Iterator for Counter {
|
|
|
|
//! // we will be counting with usize
|
|
|
|
//! type Item = usize;
|
|
|
|
//!
|
|
|
|
//! // next() is the only required method
|
|
|
|
//! fn next(&mut self) -> Option<usize> {
|
2018-11-20 11:22:26 -06:00
|
|
|
//! // Increment our count. This is why we started at zero.
|
2016-04-18 13:08:27 -05:00
|
|
|
//! self.count += 1;
|
|
|
|
//!
|
2018-11-20 11:22:26 -06:00
|
|
|
//! // Check to see if we've finished counting or not.
|
2016-04-18 13:08:27 -05:00
|
|
|
//! if self.count < 6 {
|
|
|
|
//! Some(self.count)
|
|
|
|
//! } else {
|
|
|
|
//! None
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // And now we can use it!
|
|
|
|
//!
|
|
|
|
//! let mut counter = Counter::new();
|
|
|
|
//!
|
|
|
|
//! let x = counter.next().unwrap();
|
|
|
|
//! println!("{}", x);
|
|
|
|
//!
|
|
|
|
//! let x = counter.next().unwrap();
|
|
|
|
//! println!("{}", x);
|
|
|
|
//!
|
|
|
|
//! let x = counter.next().unwrap();
|
|
|
|
//! println!("{}", x);
|
|
|
|
//!
|
|
|
|
//! let x = counter.next().unwrap();
|
|
|
|
//! println!("{}", x);
|
|
|
|
//!
|
|
|
|
//! let x = counter.next().unwrap();
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This will print `1` through `5`, each on their own line.
|
|
|
|
//!
|
|
|
|
//! Calling `next()` this way gets repetitive. Rust has a construct which can
|
|
|
|
//! call `next()` on your iterator, until it reaches `None`. Let's go over that
|
|
|
|
//! next.
|
|
|
|
//!
|
|
|
|
//! # for Loops and IntoIterator
|
|
|
|
//!
|
|
|
|
//! Rust's `for` loop syntax is actually sugar for iterators. Here's a basic
|
|
|
|
//! example of `for`:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let values = vec![1, 2, 3, 4, 5];
|
|
|
|
//!
|
|
|
|
//! for x in values {
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This will print the numbers one through five, each on their own line. But
|
|
|
|
//! you'll notice something here: we never called anything on our vector to
|
|
|
|
//! produce an iterator. What gives?
|
|
|
|
//!
|
|
|
|
//! There's a trait in the standard library for converting something into an
|
2017-03-12 13:04:52 -05:00
|
|
|
//! iterator: [`IntoIterator`]. This trait has one method, [`into_iter`],
|
2016-04-18 13:08:27 -05:00
|
|
|
//! which converts the thing implementing [`IntoIterator`] into an iterator.
|
|
|
|
//! Let's take a look at that `for` loop again, and what the compiler converts
|
|
|
|
//! it into:
|
|
|
|
//!
|
|
|
|
//! [`IntoIterator`]: trait.IntoIterator.html
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`into_iter`]: trait.IntoIterator.html#tymethod.into_iter
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let values = vec![1, 2, 3, 4, 5];
|
|
|
|
//!
|
|
|
|
//! for x in values {
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Rust de-sugars this into:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let values = vec![1, 2, 3, 4, 5];
|
|
|
|
//! {
|
|
|
|
//! let result = match IntoIterator::into_iter(values) {
|
|
|
|
//! mut iter => loop {
|
2017-06-13 11:36:01 -05:00
|
|
|
//! let next;
|
|
|
|
//! match iter.next() {
|
|
|
|
//! Some(val) => next = val,
|
2016-04-18 13:08:27 -05:00
|
|
|
//! None => break,
|
2017-05-27 13:20:17 -05:00
|
|
|
//! };
|
2017-06-13 11:36:01 -05:00
|
|
|
//! let x = next;
|
2017-05-27 13:20:17 -05:00
|
|
|
//! let () = { println!("{}", x); };
|
2016-04-18 13:08:27 -05:00
|
|
|
//! },
|
|
|
|
//! };
|
|
|
|
//! result
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! First, we call `into_iter()` on the value. Then, we match on the iterator
|
2017-03-12 13:04:52 -05:00
|
|
|
//! that returns, calling [`next`] over and over until we see a `None`. At
|
2016-04-18 13:08:27 -05:00
|
|
|
//! that point, we `break` out of the loop, and we're done iterating.
|
|
|
|
//!
|
|
|
|
//! There's one more subtle bit here: the standard library contains an
|
|
|
|
//! interesting implementation of [`IntoIterator`]:
|
|
|
|
//!
|
2017-06-20 02:15:16 -05:00
|
|
|
//! ```ignore (only-for-syntax-highlight)
|
2016-04-18 13:08:27 -05:00
|
|
|
//! impl<I: Iterator> IntoIterator for I
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! In other words, all [`Iterator`]s implement [`IntoIterator`], by just
|
|
|
|
//! returning themselves. This means two things:
|
|
|
|
//!
|
|
|
|
//! 1. If you're writing an [`Iterator`], you can use it with a `for` loop.
|
|
|
|
//! 2. If you're creating a collection, implementing [`IntoIterator`] for it
|
|
|
|
//! will allow your collection to be used with the `for` loop.
|
|
|
|
//!
|
|
|
|
//! # Adapters
|
|
|
|
//!
|
|
|
|
//! Functions which take an [`Iterator`] and return another [`Iterator`] are
|
|
|
|
//! often called 'iterator adapters', as they're a form of the 'adapter
|
|
|
|
//! pattern'.
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! Common iterator adapters include [`map`], [`take`], and [`filter`].
|
2016-04-18 13:08:27 -05:00
|
|
|
//! For more, see their documentation.
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`map`]: trait.Iterator.html#method.map
|
|
|
|
//! [`take`]: trait.Iterator.html#method.take
|
|
|
|
//! [`filter`]: trait.Iterator.html#method.filter
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
|
|
|
//! # Laziness
|
|
|
|
//!
|
|
|
|
//! Iterators (and iterator [adapters](#adapters)) are *lazy*. This means that
|
|
|
|
//! just creating an iterator doesn't _do_ a whole lot. Nothing really happens
|
2017-03-12 13:04:52 -05:00
|
|
|
//! until you call [`next`]. This is sometimes a source of confusion when
|
|
|
|
//! creating an iterator solely for its side effects. For example, the [`map`]
|
2016-04-18 13:08:27 -05:00
|
|
|
//! method calls a closure on each element it iterates over:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! # #![allow(unused_must_use)]
|
|
|
|
//! let v = vec![1, 2, 3, 4, 5];
|
|
|
|
//! v.iter().map(|x| println!("{}", x));
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This will not print any values, as we only created an iterator, rather than
|
|
|
|
//! using it. The compiler will warn us about this kind of behavior:
|
|
|
|
//!
|
|
|
|
//! ```text
|
2019-01-13 00:17:57 -06:00
|
|
|
//! warning: unused result that must be used: iterators are lazy and
|
2016-04-18 13:08:27 -05:00
|
|
|
//! do nothing unless consumed
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! The idiomatic way to write a [`map`] for its side effects is to use a
|
2016-04-18 13:08:27 -05:00
|
|
|
//! `for` loop instead:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let v = vec![1, 2, 3, 4, 5];
|
|
|
|
//!
|
|
|
|
//! for x in &v {
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`map`]: trait.Iterator.html#method.map
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
|
|
|
//! The two most common ways to evaluate an iterator are to use a `for` loop
|
2017-03-12 13:04:52 -05:00
|
|
|
//! like this, or using the [`collect`] method to produce a new collection.
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`collect`]: trait.Iterator.html#method.collect
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
|
|
|
//! # Infinity
|
|
|
|
//!
|
|
|
|
//! Iterators do not have to be finite. As an example, an open-ended range is
|
|
|
|
//! an infinite iterator:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let numbers = 0..;
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! It is common to use the [`take`] iterator adapter to turn an infinite
|
2016-04-18 13:08:27 -05:00
|
|
|
//! iterator into a finite one:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let numbers = 0..;
|
|
|
|
//! let five_numbers = numbers.take(5);
|
|
|
|
//!
|
|
|
|
//! for number in five_numbers {
|
|
|
|
//! println!("{}", number);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This will print the numbers `0` through `4`, each on their own line.
|
|
|
|
//!
|
2018-01-18 09:28:10 -06:00
|
|
|
//! Bear in mind that methods on infinite iterators, even those for which a
|
2018-01-19 15:16:34 -06:00
|
|
|
//! result can be determined mathematically in finite time, may not terminate.
|
|
|
|
//! Specifically, methods such as [`min`], which in the general case require
|
|
|
|
//! traversing every element in the iterator, are likely not to return
|
|
|
|
//! successfully for any infinite iterators.
|
2018-01-18 11:49:32 -06:00
|
|
|
//!
|
|
|
|
//! ```no_run
|
2018-01-21 13:45:27 -06:00
|
|
|
//! let ones = std::iter::repeat(1);
|
|
|
|
//! let least = ones.min().unwrap(); // Oh no! An infinite loop!
|
|
|
|
//! // `ones.min()` causes an infinite loop, so we won't reach this point!
|
|
|
|
//! println!("The smallest number one is {}.", least);
|
2018-01-18 09:28:10 -06:00
|
|
|
//! ```
|
2016-04-18 13:08:27 -05:00
|
|
|
//!
|
2017-03-12 13:04:52 -05:00
|
|
|
//! [`take`]: trait.Iterator.html#method.take
|
2018-01-18 09:28:10 -06:00
|
|
|
//! [`min`]: trait.Iterator.html#method.min
|
2016-04-18 13:08:27 -05:00
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
2018-11-18 12:39:23 -06:00
|
|
|
use ops::Try;
|
2016-04-18 13:08:27 -05:00
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::iterator::Iterator;
|
|
|
|
|
|
|
|
#[unstable(feature = "step_trait",
|
|
|
|
reason = "likely to be replaced by finer-grained traits",
|
2017-05-23 05:08:18 -05:00
|
|
|
issue = "42168")]
|
2016-04-18 13:08:27 -05:00
|
|
|
pub use self::range::Step;
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::sources::{Repeat, repeat};
|
2018-05-30 00:30:16 -05:00
|
|
|
#[stable(feature = "iterator_repeat_with", since = "1.28.0")]
|
2018-02-12 01:25:39 -06:00
|
|
|
pub use self::sources::{RepeatWith, repeat_with};
|
2016-04-18 13:08:27 -05:00
|
|
|
#[stable(feature = "iter_empty", since = "1.2.0")]
|
|
|
|
pub use self::sources::{Empty, empty};
|
|
|
|
#[stable(feature = "iter_once", since = "1.2.0")]
|
|
|
|
pub use self::sources::{Once, once};
|
2019-01-13 14:24:15 -06:00
|
|
|
#[unstable(feature = "iter_once_with", issue = "57581")]
|
2019-01-13 04:16:14 -06:00
|
|
|
pub use self::sources::{OnceWith, once_with};
|
2018-11-15 07:33:47 -06:00
|
|
|
#[unstable(feature = "iter_unfold", issue = "55977")]
|
2018-11-15 07:23:20 -06:00
|
|
|
pub use self::sources::{Unfold, unfold, Successors, successors};
|
2016-04-18 13:08:27 -05:00
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-06-28 10:56:56 -05:00
|
|
|
pub use self::traits::{FromIterator, IntoIterator, DoubleEndedIterator, Extend};
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::traits::{ExactSizeIterator, Sum, Product};
|
2018-03-03 07:15:28 -06:00
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
2016-08-13 13:42:36 -05:00
|
|
|
pub use self::traits::FusedIterator;
|
2016-11-03 18:24:59 -05:00
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
2016-10-20 07:07:06 -05:00
|
|
|
pub use self::traits::TrustedLen;
|
2016-04-18 13:08:27 -05:00
|
|
|
|
2018-12-17 16:23:12 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::adapters::{Rev, Cycle, Chain, Zip, Map, Filter, FilterMap, Enumerate};
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::adapters::{Peekable, SkipWhile, TakeWhile, Skip, Take, Scan, FlatMap};
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use self::adapters::{Fuse, Inspect};
|
|
|
|
#[stable(feature = "iter_cloned", since = "1.1.0")]
|
|
|
|
pub use self::adapters::Cloned;
|
|
|
|
#[stable(feature = "iterator_step_by", since = "1.28.0")]
|
|
|
|
pub use self::adapters::StepBy;
|
|
|
|
#[stable(feature = "iterator_flatten", since = "1.29.0")]
|
|
|
|
pub use self::adapters::Flatten;
|
|
|
|
#[unstable(feature = "iter_copied", issue = "57127")]
|
|
|
|
pub use self::adapters::Copied;
|
|
|
|
|
|
|
|
use self::adapters::{flatten_compat, ChainState, ZipImpl};
|
|
|
|
|
2016-04-18 13:08:27 -05:00
|
|
|
mod iterator;
|
|
|
|
mod range;
|
|
|
|
mod sources;
|
|
|
|
mod traits;
|
2018-12-17 16:23:12 -06:00
|
|
|
mod adapters;
|
2016-04-18 13:08:27 -05:00
|
|
|
|
2017-10-23 00:47:27 -05:00
|
|
|
/// Used to make try_fold closures more like normal loops
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum LoopState<C, B> {
|
|
|
|
Continue(C),
|
|
|
|
Break(B),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, B> Try for LoopState<C, B> {
|
|
|
|
type Ok = C;
|
|
|
|
type Error = B;
|
|
|
|
#[inline]
|
|
|
|
fn into_result(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
match self {
|
|
|
|
LoopState::Continue(y) => Ok(y),
|
|
|
|
LoopState::Break(x) => Err(x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn from_error(v: Self::Error) -> Self { LoopState::Break(v) }
|
|
|
|
#[inline]
|
|
|
|
fn from_ok(v: Self::Ok) -> Self { LoopState::Continue(v) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, B> LoopState<C, B> {
|
|
|
|
#[inline]
|
|
|
|
fn break_value(self) -> Option<B> {
|
|
|
|
match self {
|
|
|
|
LoopState::Continue(..) => None,
|
|
|
|
LoopState::Break(x) => Some(x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Try> LoopState<R::Ok, R> {
|
|
|
|
#[inline]
|
|
|
|
fn from_try(r: R) -> Self {
|
|
|
|
match Try::into_result(r) {
|
|
|
|
Ok(v) => LoopState::Continue(v),
|
|
|
|
Err(v) => LoopState::Break(Try::from_error(v)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn into_try(self) -> R {
|
|
|
|
match self {
|
|
|
|
LoopState::Continue(v) => Try::from_ok(v),
|
|
|
|
LoopState::Break(v) => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|