Module rayon
Data parallelism library for Rust.
rayon is a data-parallelism library for Rust that makes it easy to convert
sequential computations into parallel ones with minimal changes to existing code.
The crate provides parallel versions of common iterator methods through the
ParallelIterator trait, allowing you to simply change .iter() to .par_iter()
to parallelize operations. Rayon uses work-stealing to efficiently distribute
computations across CPU cores.
Key features include parallel iteration with par_iter, parallel collection
operations like map, filter, and reduce, parallel sorting with
par_sort, and parallel searching. The library also provides join for
fork-join parallelism and scope for structured parallelism with lifetimes.
Rayon automatically manages thread pools and work distribution, making parallelism accessible without manual thread management. It's particularly effective for CPU-bound tasks that can be decomposed into independent units.
Rayon's global ThreadPool is the recommended threadpool implementation
in rustmax for general-purpose parallel computation. It provides excellent
work-stealing performance and integrates seamlessly with rayon's parallel
iterators.
Examples
Basic parallel iteration:
use *;
let data = vec!;
let sum: i32 = data.par_iter.map.sum;
assert_eq!; // 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64
Parallel filtering and collection:
use *;
let numbers = .;
let evens: = numbers
.par_iter
.filter
.map
.collect;
assert_eq!; // 2 * 2
assert_eq!; // 4 * 2
Parallel reduction with custom operation:
use *;
let data = vec!;
let max = data.par_iter.cloned.reduce;
assert_eq!;
Using rayon's threadpool for custom parallel work:
use ThreadPoolBuilder;
let pool = new.num_threads.build.unwrap;
let result = pool.install;
assert_eq!;
Modules
-
array
Parallel iterator types for arrays (
[T; N]) - collections Parallel iterator types for standard collections
- iter Traits for writing parallel programs using an iterator-style interface
- option Parallel iterator types for options
-
prelude
The rayon prelude imports the various
ParallelIteratortraits. The intention is that one can includeuse rayon::prelude::*and have easy access to the various traits and methods you will need. -
range
Parallel iterator types for ranges,
the type for values created by
a..bexpressions -
range_inclusive
Parallel iterator types for inclusive ranges,
the type for values created by
a..=bexpressions - result Parallel iterator types for results
- slice Parallel iterator types for slices
- str Parallel iterator types for strings
-
string
This module contains the parallel iterator types for owned strings
(
String). You will rarely need to interact with it directly unless you have need to name one of the iterator types. -
vec
Parallel iterator types for vectors (
Vec<T>)