Rayon parallelizes CPU-bound iterators: swap in par_iter (and friends), keep order when Rayon documents it, and let the type system reject data races at compile time.
Serial to parallel
A synchronous sum:
1 | fn main() { |
Parallel version: into_iter → into_par_iter.
Rayon parallelizes the work but still produces the same reduction result (order of the range is preserved for this pattern):
1 | use rayon::prelude::*; |
On a 10-core M1 Pro, divan showed a clear win for this toy loop:
| Benchmark | Fastest | Slowest | Median | Mean | Samples | Iterations |
|---|---|---|---|---|---|---|
| iter | 549.2 µs | 1.244 ms | 687.4 µs | 738.5 µs | 100 | 100 |
| par_iter | 195 µs | 488.1 µs | 315.1 µs | 321.9 µs | 100 | 100 |
What’s under the hood
Rayon backs parallel iterators with a work-stealing thread pool. By default the pool size matches logical CPU cores.
For each parallel job, Rayon splits work into subtasks (roughly sized for the pool) and queues them per thread.
If some threads are idle while others still have local queues, idle workers steal tasks from busy queues so cores stay fed.
Partial results are merged pairwise until the full answer is ready.
Splitting plus stealing is the divide-and-conquer bit that makes par_iter feel magical from the call site.
join
Lower in the stack, rayon::join runs two closures in parallel and returns both outputs:
1 | use rayon::prelude::*; |
par_bridge
par_iter shines on indexed or slice-backed data. For blocking sources—channels, files, network—use par_bridge.
There’s overhead: items are pulled one at a time and handed to the pool, and order is not preserved. Sort or reorder afterward if you care.
1 | use rayon::iter::ParallelBridge; |
Not every for loop deserves Rayon—thread scheduling has cost. For cheap scans, stay serial. For heavier per-item work, into_par_iter is often the fastest one-line change.
Background: Rayon: data parallelism in Rust
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
