Rust Iterator overview
Iterators are a powerful concept in Rust. This website gives an overview and helps you select the best method.
For an introduction, read the chapter in the book. Remember: Iterators (and adapters) are lazy. Creating an iterator does nothing, so you need to consume it to perform the iteration.
Constructors
I want to create an iterator from...
a function
Creates an iterator with the provided closure as its next method.
Creates an iterator that yields an element exactly once.
Creates an iterator that lazily generates a value exactly once by invoking the provided closure.
Creates a new iterator that endlessly repeats a single element.
Creates a new iterator that repeats a single element a given number of times.
Creates a new iterator that repeats elements endlessly by applying the provided closure.
Creates an iterator which, starting from an initial item, computes each successive item from the preceding one.
an array/slice/collection
An iterator that borrows the elements
An iterator that mutably borrows the elements
Moves the array/slice/collection, returning an owned iterator over its elements.
a string
Returns an iterator over the chars of a string slice, and their positions.
Returns an iterator over substrings of this string slice, separated by characters matched by a pattern.
Also see split_whitespace,
split_ascii_whitespace,
split_inclusive,
rsplit, etc.
Returns an iterator over the disjoint matches of a pattern within the given string slice.
Also see rmatches,
match_indices,
rmatch_indices
a HashMap/BTreeMap
Moves the HashMap, returning an iterator over the owned keys in arbitrary order.
Moves the HashMap, returning an iterator over the owned values in arbitrary order.
An iterator visiting all values mutably in arbitrary order.
CLI arguments
Returns the arguments that this program was started with (normally passed via the command line).
environment variables
Returns an iterator of (variable, value) pairs of strings, for all the environment variables of the current process.
a file path
Produces an iterator over Path and its ancestors.
Produces an iterator over the Components of the path.
Returns an iterator over the entries within a directory.
There's also a free function.
Read/BufRead
TcpListener
Returns an iterator over the connections being received on this listener.
Adapters
Filter Remove items from the iterator
Creates an iterator which uses a closure to determine if an element should be yielded.
See also: filter_map
Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
Creates an iterator that skips elements based on a predicate.
Creates an iterator that yields the first n elements, or fewer if the underlying iterator ends sooner.
Creates an iterator that yields elements while the given predicate returns true.
Expand Add items to the iterator
Takes two iterators and creates a new iterator over both in sequence.
Transform Modify the items in the iterator
Creates an iterator which gives the current iteration count as well as the next value.
Takes a closure and creates an iterator which calls that closure on each element.
‘Zips up’ two iterators into a single iterator of pairs. There's also a free function.
See also: unzip
Creates an iterator that works like map, but flattens nested structure.
See also: map
Creates an iterator that both yields elements based on a predicate and maps.
See also: take_while
An iterator adapter which, like fold, holds internal state, but unlike fold, produces a new iterator.
See also: fold
Misc.
Does something with each element of an iterator, passing the value on.
Creates an iterator which can use the peek and peek_mut methods to look at the next element of the iterator without consuming it.
Consumers
Select Pick an item from the iterator
Search Find an item matching a predicate
Searches for an element of an iterator that satisfies a predicate.
Applies a function to the elements of the iterator and returns the first non-none result.
See also: map
Searches for an element in an iterator, returning its index.
Searches for an element in an iterator from the right, returning its index.
See also: rev
Validate Check if the iterator meets a certain condition
Tests if every element of the iterator matches a predicate.
Tests if any element of the iterator matches a predicate.
Checks if the elements of this iterator are sorted.
Checks if the elements of this iterator are sorted using the given comparator function.
Checks if the elements of this iterator are sorted using the given key extraction function.
Accumulate Condense the iterator into a single value
Folds every element into an accumulator by applying an operation, returning the final result.
Reduces the elements to a single one, by repeatedly applying a reducing operation.
Returns the element that gives the maximum value with respect to the specified comparison function.
Returns the element that gives the maximum value from the specified key extraction function.
Returns the element that gives the minimum value with respect to the specified comparison function.
Returns the element that gives the minimum value from the specified key extraction function.
An iterator method that applies a function as long as it returns successfully, producing a single, final value.
See also: fold
Consumes an iterator, creating two collections from it.
Compare Lexicographically compare two iterators
Lexicographically compares the elements of this Iterator with those of another.
Lexicographically compares the PartialOrd elements of this Iterator with those of another. The comparison works like short-circuit evaluation.
Determines if the elements of this Iterator are equal to those of another.
Determines if the elements of this Iterator are not equal to those of another.
Determines if the elements of this Iterator are lexicographically greater than or equal to those of another.
Determines if the elements of this Iterator are lexicographically greater than those of another.
Determines if the elements of this Iterator are lexicographically less or equal to those of another.
Determines if the elements of this Iterator are lexicographically less than those of another.
Misc.
Calls a closure on each element of an iterator.
An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error.
See also: for_each
Other
Returns the bounds on the remaining length of the iterator.