Module iter
Traits for writing parallel programs using an iterator-style interface
You will rarely need to interact with this module directly unless you have need to name one of the iterator types.
Parallel iterators make it easy to write iterator-like chains that
execute in parallel: typically all you have to do is convert the
first .iter() (or iter_mut(), into_iter(), etc) method into
par_iter() (or par_iter_mut(), into_par_iter(), etc). For
example, to compute the sum of the squares of a sequence of
integers, one might write:
use *;
Or, to increment all the integers in a slice, you could write:
use *;
To use parallel iterators, first import the traits by adding
something like use rayon::prelude::* to your module. You can
then call par_iter, par_iter_mut, or into_par_iter to get a
parallel iterator. Like a regular iterator, parallel
iterators work by first constructing a computation and then
executing it.
In addition to par_iter() and friends, some types offer other
ways to create (or consume) parallel iterators:
- Slices (
&[T],&mut [T]) offer methods likepar_splitandpar_windows, as well as various parallel sorting operations. See theParallelSlicetrait for the full list. - Strings (
&str) offer methods likepar_splitandpar_lines. See theParallelStringtrait for the full list. - Various collections offer
par_extend, which grows a collection given a parallel iterator. (If you don't have a collection to extend, you can usecollect()to create a new one from scratch.)
To see the full range of methods available on parallel iterators,
check out the ParallelIterator and IndexedParallelIterator
traits.
If you'd like to build a custom parallel iterator, or to write your own combinator, then check out the split function and the plumbing module.
Note: Several of the ParallelIterator methods rely on a Try trait which
has been deliberately obscured from the public API. This trait is intended
to mirror the unstable std::ops::Try with implementations for Option and
Result, where Some/Ok values will let those iterators continue, but
None/Err values will exit early.
A note about object safety: It is currently not possible to wrap
a ParallelIterator (or any trait that depends on it) using a
Box<dyn ParallelIterator> or other kind of dynamic allocation,
because ParallelIterator is not object-safe.
(This keeps the implementation simpler and allows extra optimizations.)
Modules
-
plumbing
Traits and functions used to implement parallel iteration. These are
low-level details -- users of parallel iterators should not need to
interact with them directly. See [the
plumbingREADME][r] for a general overview.
Structs
-
Chain
Chainis an iterator that joinsbafterain one continuous iterator. This struct is created by the [chain()] method onParallelIterator -
Chunks
Chunksis an iterator that groups elements of an underlying iterator. -
Cloned
Clonedis an iterator that clones the elements of an underlying iterator. -
Copied
Copiedis an iterator that copies the elements of an underlying iterator. -
Empty
Iterator adaptor for the
empty()function. -
Enumerate
Enumerateis an iterator that returns the current count along with the element. This struct is created by the [enumerate()] method onIndexedParallelIterator -
ExponentialBlocks
ExponentialBlocksis a parallel iterator that consumes itself as a sequence of parallel blocks of increasing sizes (exponentially). -
Filter
Filtertakes a predicatefilter_opand filters out elements that match. This struct is created by the [filter()] method onParallelIterator -
FilterMap
FilterMapcreates an iterator that usesfilter_opto both filter and map elements. This struct is created by the [filter_map()] method onParallelIterator. -
FlatMap
FlatMapmaps each element to a parallel iterator, then flattens these iterators together. This struct is created by the [flat_map()] method onParallelIterator -
FlatMapIter
FlatMapItermaps each element to a serial iterator, then flattens these iterators together. This struct is created by the [flat_map_iter()] method onParallelIterator -
Flatten
Flattenturns each element to a parallel iterator, then flattens these iterators together. This struct is created by the [flatten()] method onParallelIterator. -
FlattenIter
FlattenIterturns each element to a serial iterator, then flattens these iterators together. This struct is created by the [flatten_iter()] method onParallelIterator. -
Fold
Foldis an iterator that applies a function over an iterator producing a single value. This struct is created by the [fold()] method onParallelIterator -
FoldChunks
FoldChunksis an iterator that groups elements of an underlying iterator and applies a function over them, producing a single value for each group. -
FoldChunksWith
FoldChunksWithis an iterator that groups elements of an underlying iterator and applies a function over them, producing a single value for each group. -
FoldWith
FoldWithis an iterator that applies a function over an iterator producing a single value. This struct is created by the [fold_with()] method onParallelIterator -
Inspect
Inspectis an iterator that calls a function with a reference to each element before yielding it. -
Interleave
Interleaveis an iterator that interleaves elements of iteratorsiandjin one continuous iterator. This struct is created by the [interleave()] method onIndexedParallelIterator -
InterleaveShortest
InterleaveShortestis an iterator that works similarly toInterleave, but this version stops returning elements once one of the iterators run out. -
Intersperse
Intersperseis an iterator that inserts a particular item between each item of the adapted iterator. This struct is created by the [intersperse()] method onParallelIterator -
IterBridge
IterBridgeis a parallel iterator that wraps a sequential iterator. -
Map
Mapis an iterator that transforms the elements of an underlying iterator. -
MapInit
MapInitis an iterator that transforms the elements of an underlying iterator. -
MapWith
MapWithis an iterator that transforms the elements of an underlying iterator. -
MaxLen
MaxLenis an iterator that imposes a maximum length on iterator splits. This struct is created by the [with_max_len()] method onIndexedParallelIterator -
MinLen
MinLenis an iterator that imposes a minimum length on iterator splits. This struct is created by the [with_min_len()] method onIndexedParallelIterator -
MultiZip
MultiZipis an iterator that zips up a tuple of parallel iterators to produce tuples of their items. -
Once
Iterator adaptor for the
once()function. -
PanicFuse
PanicFuseis an adaptor that wraps an iterator with a fuse in case of panics, to halt all threads as soon as possible. -
Positions
Positionstakes a predicatepredicateand filters out elements that match, yielding their indices. -
Repeat
Iterator adaptor for the
repeat()function. -
RepeatN
Iterator adaptor for the
repeatn()function. -
Rev
Revis an iterator that produces elements in reverse order. This struct is created by the [rev()] method onIndexedParallelIterator -
Skip
Skipis an iterator that skips over the firstnelements. This struct is created by the [skip()] method onIndexedParallelIterator -
SkipAny
SkipAnyis an iterator that skips overnelements from anywhere inI. This struct is created by the [skip_any()] method onParallelIterator -
SkipAnyWhile
SkipAnyWhileis an iterator that skips over elements from anywhere inIuntil the callback returnsfalse. This struct is created by the [skip_any_while()] method onParallelIterator -
Split
Splitis a parallel iterator using arbitrary data and a splitting function. This struct is created by the [split()] function. -
StepBy
StepByis an iterator that skipsnelements between each yield, wherenis the given step. This struct is created by the [step_by()] method onIndexedParallelIterator -
Take
Takeis an iterator that iterates over the firstnelements. This struct is created by the [take()] method onIndexedParallelIterator -
TakeAny
TakeAnyis an iterator that iterates overnelements from anywhere inI. This struct is created by the [take_any()] method onParallelIterator -
TakeAnyWhile
TakeAnyWhileis an iterator that iterates over elements from anywhere inIuntil the callback returnsfalse. This struct is created by the [take_any_while()] method onParallelIterator -
TryFold
TryFoldis an iterator that applies a function over an iterator producing a single value. This struct is created by the [try_fold()] method onParallelIterator -
TryFoldWith
TryFoldWithis an iterator that applies a function over an iterator producing a single value. This struct is created by the [try_fold_with()] method onParallelIterator -
UniformBlocks
UniformBlocksis a parallel iterator that consumes itself as a sequence of parallel blocks of constant sizes. -
Update
Updateis an iterator that mutates the elements of an underlying iterator before they are yielded. -
WalkTree
ParallelIterator for arbitrary tree-shaped patterns.
Returned by the [
walk_tree()] function. -
WalkTreePostfix
ParallelIterator for arbitrary tree-shaped patterns.
Returned by the [
walk_tree_postfix()] function. -
WalkTreePrefix
ParallelIterator for arbitrary tree-shaped patterns.
Returned by the [
walk_tree_prefix()] function. -
WhileSome
WhileSomeis an iterator that yields theSomeelements of an iterator, halting as soon as anyNoneis produced. -
Zip
Zipis an iterator that zips upaandbinto a single iterator of pairs. This struct is created by the [zip()] method onIndexedParallelIterator -
ZipEq
An
IndexedParallelIteratorthat iterates over two parallel iterators of equal length simultaneously.
Enums
Traits
-
FromParallelIterator
FromParallelIteratorimplements the creation of a collection from aParallelIterator. By implementingFromParallelIteratorfor a given type, you define how it will be created from an iterator. - IndexedParallelIterator An iterator that supports "random access" to its data, meaning that you can split it at arbitrary indices and draw data from those points.
-
IntoParallelIterator
IntoParallelIteratorimplements the conversion to aParallelIterator. -
IntoParallelRefIterator
IntoParallelRefIteratorimplements the conversion to aParallelIterator, providing shared references to the data. -
IntoParallelRefMutIterator
IntoParallelRefMutIteratorimplements the conversion to aParallelIterator, providing mutable references to the data. -
ParallelBridge
Conversion trait to convert an
Iteratorto aParallelIterator. -
ParallelDrainFull
ParallelDrainFullcreates a parallel iterator that moves all items from a collection while retaining the original capacity. -
ParallelDrainRange
ParallelDrainRangecreates a parallel iterator that moves a range of items from a collection while retaining the original capacity. -
ParallelExtend
ParallelExtendextends an existing collection with items from aParallelIterator. - ParallelIterator Parallel version of the standard iterator trait.
Functions
- empty Creates a parallel iterator that produces nothing.
- once Creates a parallel iterator that produces an element exactly once.
-
repeat
Creates a parallel iterator that endlessly repeats
elt(by cloning it). Note that this iterator has "infinite" length, so typically you would want to useziportakeor some other means to shorten it, or consider using therepeatn()function instead. -
repeatn
Creates a parallel iterator that produces
nrepeats ofelt(by cloning it). -
split
The
splitfunction takes arbitrary data and a closure that knows how to split it, and turns this into aParallelIterator. -
walk_tree
Create a tree like parallel iterator from an initial root node.
The
children_offunction should take a node and iterate on all of its child nodes. The best parallelization is obtained when the tree is balanced but we should also be able to handle harder cases. -
walk_tree_postfix
Create a tree like postfix parallel iterator from an initial root node.
The
children_offunction should take a node and iterate on all of its child nodes. The best parallelization is obtained when the tree is balanced but we should also be able to handle harder cases. -
walk_tree_prefix
Create a tree-like prefix parallel iterator from an initial root node.
The
children_offunction should take a node and return an iterator over its child nodes. The best parallelization is obtained when the tree is balanced but we should also be able to handle harder cases.