Rayon and Data Parallelism
Hand-rolled std::thread code gives you full control at the cost of plenty of boilerplate. For data-parallel workloads — same operation, many independent items — rayon flattens the API down to almost nothing while delivering near-optimal performance.
What Is rayon?
rayon is a data-parallelism library that turns sequential iterators into parallel ones with a one-word swap. Under the hood, it runs a thread pool and uses work stealing to keep every core busy.
use rayon::prelude::*;
fn main() { let numbers: Vec<i64> = (1..=1_000_000).collect();
// Sequential let sum_seq: i64 = numbers.iter().sum();
// Parallel - just change iter() to par_iter() let sum_par: i64 = numbers.par_iter().sum();
assert_eq!(sum_seq, sum_par); println!("Sum: {}", sum_par);}rayon's trick lives in par_iter(). It chunks the data, dispatches the chunks to worker threads, and stitches the results back together. The API mirrors plain iterators, so going parallel is often a single keystroke.
Parallel Iterators
rayon supports most iterator adapters:
use rayon::prelude::*;
fn main() { let data: Vec<i32> = (1..=100).collect();
// Parallel map + filter + collect let result: Vec<i32> = data.par_iter() .filter(|&&x| x % 3 == 0) .map(|&x| x * x) .collect();
println!("Count: {}", result.len());
// Parallel for_each data.par_iter().for_each(|&x| { // Process each element in parallel // Be careful with side effects! let _ = x * x; });
// Parallel find let found = data.par_iter().find_any(|&&x| x == 42); println!("Found 42: {}", found.is_some());}Parallel Sorting
use rayon::prelude::*;
fn main() { let mut data: Vec<i32> = (0..1_000_000) .rev() .collect();
// Parallel sort data.par_sort();
// Parallel sort with custom comparator data.par_sort_by(|a, b| b.cmp(a)); // Descending
// Parallel sort by key let mut strings = vec!["banana", "apple", "cherry", "date"]; strings.par_sort_by_key(|s| s.len());}rayon's parallel sort is built on a merge sort variant that scales nicely across cores.
Work Stealing
rayon runs a work-stealing scheduler. Each thread owns a deque of tasks. Idle threads pinch work from a busy thread's deque. The result: load balances itself, even when individual tasks differ wildly in cost.
use rayon::prelude::*;
fn variable_work(n: u64) -> u64 { // Some items take much longer than others (0..n).sum()}
fn main() { let items: Vec<u64> = vec![100, 1000, 10, 100000, 50, 500000, 1];
// Work stealing balances the load automatically let results: Vec<u64> = items.par_iter() .map(|&n| variable_work(n)) .collect();
println!("Results: {:?}", results);}When Parallelism Helps vs Hurts
Parallelism has overheads: you pay for synchronisation, splitting the work, and merging results back. On small workloads, that tax dwarfs the speedup:
use rayon::prelude::*;use std::time::Instant;
fn benchmark<F: Fn()>(name: &str, f: F) { let start = Instant::now(); f(); println!("{}: {:?}", name, start.elapsed());}
fn main() { let small: Vec<i32> = (0..100).collect(); let large: Vec<i32> = (0..10_000_000).collect();
// Small data: sequential is faster (parallelism overhead dominates) benchmark("Small sequential", || { let _: i64 = small.iter().map(|&x| x as i64 * x as i64).sum(); }); benchmark("Small parallel", || { let _: i64 = small.par_iter().map(|&x| x as i64 * x as i64).sum(); });
// Large data: parallel is faster benchmark("Large sequential", || { let _: i64 = large.iter().map(|&x| x as i64 * x as i64).sum(); }); benchmark("Large parallel", || { let _: i64 = large.par_iter().map(|&x| x as i64 * x as i64).sum(); });}Run it with cargo run --release. A debug build leaves the sequential loop unoptimised, and the comparison tells you very little.
Rules of thumb:
- There's no magic item count: the less work each item does, the more items you need before the parallel overhead pays off
- CPU-bound work benefits most from parallelism
- I/O-bound work is better served by async (tokio)
- Measure before assuming parallelism helps
rayon won't run in the runner (no crates, no threads), but the trade-off underneath it is plain arithmetic. This model splits the per-item work across the cores and charges a fixed price for going parallel. All three inputs are invented, so treat them as dials.
Custom Parallel Computation with join
For recursive parallelism, rayon provides join:
use rayon;
fn parallel_quicksort(arr: &mut [i32]) { if arr.len() <= 32 { arr.sort(); // Small arrays: use sequential sort return; }
let pivot = partition(arr); let (left, right) = arr.split_at_mut(pivot);
rayon::join( || parallel_quicksort(left), || parallel_quicksort(&mut right[1..]), );}
fn partition(arr: &mut [i32]) -> usize { let pivot = arr[arr.len() - 1]; let mut i = 0; for j in 0..arr.len() - 1 { if arr[j] <= pivot { arr.swap(i, j); i += 1; } } arr.swap(i, arr.len() - 1); i}rayon::join may execute both closures in parallel, and it never spawns a thread to do it. The calling worker runs the first closure itself and pushes the second onto its own deque, where an idle worker can steal it. If nobody has stolen it by the time the first closure finishes, the caller pops it back and runs it too. On a busy pool the two halves just run one after the other on the same thread, so the pool is never over-subscribed.
Configuring the Thread Pool
use rayon::ThreadPoolBuilder;
fn main() { // Global thread pool (set once) ThreadPoolBuilder::new() .num_threads(4) .build_global() .unwrap();
// Or create a custom pool let pool = ThreadPoolBuilder::new() .num_threads(2) .build() .unwrap();
pool.install(|| { // Code here runs on the custom pool let sum: i64 = (0..1000).into_par_iter().sum(); println!("Sum: {}", sum); });}Parallel File Processing
A practical example: processing files in parallel:
use rayon::prelude::*;use std::fs;use std::path::PathBuf;
fn count_lines(path: &PathBuf) -> usize { fs::read_to_string(path) .map(|content| content.lines().count()) .unwrap_or(0)}
fn main() { let files: Vec<PathBuf> = fs::read_dir("src") .unwrap() .filter_map(|e| e.ok()) .map(|e| e.path()) .filter(|p| p.extension().map_or(false, |e| e == "rs")) .collect();
let total_lines: usize = files.par_iter() .map(count_lines) .sum();
println!("Total lines across {} files: {}", files.len(), total_lines);}rayon makes parallelism in Rust feel almost lazy. For most data-parallel work, swapping .iter() for .par_iter() is the entire diff.