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 thestruct
itself. For more detail about why, see ‘Implementing Iterator’.
Iterator
The heart and soul of this module is theIterator
trait. The core of
Iterator
looks like this:
next
, which when called, returns an
Option. Calling 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.
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.
Forms of iteration
There is currently only one common method which can create iterators from a collection:into_iter()
, which iterates overT
.
Implementing Iterator
Creating an iterator of your own involves two steps: creating astruct
to
hold the iterator’s state, and then implementing 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
:
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 core library contains an
interesting implementation of IntoIterator
:
(only-for-syntax-highlight)
Iterator
s implement IntoIterator
, by just
returning themselves. This means two things:
- If you’re writing an
Iterator
, you can use it with afor
loop. - If you’re creating a collection, implementing
IntoIterator
for it will allow your collection to be used with thefor
loop.
Adapters
Functions which take anIterator
and return another Iterator
are
often called ‘iterator adapters’, as they’re a form of the ‘adapter
pattern’.
Common iterators adapters include map
, enumerate
and zip
.
Laziness
Iterators (and iterator adapters) are lazy. This means that just creating an iterator doesn’t do a whole lot. Nothing really happens until you callnext
. 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:
Modules
adapters | — |
traits | — |
Re-exports:
-
Free functions
zip | Converts the arguments to iterators and zips them. See the documentation of Iterator::zip for more… |
-
Traits
PeekableTrait | — |
Extend | Extend a collection with the contents of an iterator. Iterators produce a series of values, and collections can also be thought of as a series of values. The Extend … |
FromIterator | Conversion from an Iterator . By implementing FromIterator for a type, you define how it will be… |
IntoIterator | Conversion into an Iterator . By implementing IntoIterator for a type, you define how it will be… |
Iterator | A trait for dealing with iterators. This is the main iterator trait. For more about the concept of iterators generally, please see the module-level documentation… |