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:
This module is largely organized by type:
struct, rather than the struct itself. For more detail about why,
see 'Implementing Iterator'.That's it! Let's dig into iterators.
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>; }Run
An iterator has a method, next, which when called, returns an
Option<Item>. next will return Some(Item) as long as there
are elements, and once they've all been exhausted, will return None to
indicate that iteration is finished. Individual iterators may choose to
resume iteration, and so calling next again may or may not eventually
start returning Some(Item) again at some point.
Iterator's full definition includes a number of other methods as well,
but they are default methods, built on top of next, and so you get
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 section below for more details.
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.
Creating an iterator of your own involves two steps: creating a struct to
hold the iterator's state, and then implementing Iterator for that
struct. This is why there are so many structs 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:
struct Counter { count: usize, } impl Counter { fn new() -> Counter { Counter { count: 0 } } } impl Iterator for Counter { type Item = usize; fn next(&mut self) -> Option<usize> { self.count += 1; if self.count < 6 { Some(self.count) } else { None } } } 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);Run
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.
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); }Run
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
iterator: IntoIterator. This trait has one method, into_iter,
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:
let values = vec![1, 2, 3, 4, 5]; for x in values { println!("{}", x); }Run
Rust de-sugars this into:
let values = vec![1, 2, 3, 4, 5]; { let result = match IntoIterator::into_iter(values) { mut iter => loop { let next; match iter.next() { Some(val) => next = val, None => break, }; let x = next; let () = { println!("{}", x); }; }, }; result }Run
First, we call into_iter() on the value. Then, we match on the iterator
that returns, calling next over and over until we see a None. At
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:
ⓘThis example is not tested
impl<I: Iterator> IntoIterator for IRun
In other words, all Iterators implement IntoIterator, by just
returning themselves. This means two things:
Iterator, you can use it with a for loop.IntoIterator for it
will allow your collection to be used with the for loop.Functions which take an Iterator and return another Iterator are
often called 'iterator adapters', as they're a form of the 'adapter
pattern'.
Common iterator adapters include map, take, and filter.
For more, see their documentation.
Iterators (and iterator adapters) are lazy. This means that
just creating an iterator doesn't do a whole lot. Nothing really happens
until you call next. This is sometimes a source of confusion when
creating an iterator solely for its side effects. For example, the map
method calls a closure on each element it iterates over:
let v = vec![1, 2, 3, 4, 5]; v.iter().map(|x| println!("{}", x));Run
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:
warning: unused result which must be used: iterator adaptors are lazy and
do nothing unless consumed
The idiomatic way to write a map for its side effects is to use a
for loop instead:
let v = vec![1, 2, 3, 4, 5]; for x in &v { println!("{}", x); }Run
The two most common ways to evaluate an iterator are to use a for loop
like this, or using the collect method to produce a new collection.
Iterators do not have to be finite. As an example, an open-ended range is an infinite iterator:
let numbers = 0..;Run
It is common to use the take iterator adapter to turn an infinite
iterator into a finite one:
let numbers = 0..; let five_numbers = numbers.take(5); for number in five_numbers { println!("{}", number); }Run
This will print the numbers 0 through 4, each on their own line.
Bear in mind that methods on infinite iterators, even those for which a
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.
let ones = std::iter::repeat(1); let least = ones.min().unwrap(); println!("The smallest number one is {}.", least);Run