Rayon — Data Parallelism Library for Rust
Rayon makes it trivial to convert sequential Rust code into parallel code. Replace .iter() with .par_iter() and Rayon automatically distributes work across all CPU cores — with zero data races guaranteed by the Rust type system.
What it is
Rayon is the standard data parallelism library for Rust. It provides parallel iterators that mirror the standard Rust iterator API but distribute work across all available CPU cores automatically. The key advantage is that Rust's type system guarantees no data races at compile time, making parallelism safe by construction.
Rayon targets Rust developers who need to speed up CPU-bound computations without manual thread management. It is used by ripgrep, Polars, Servo, and many other performance-critical Rust projects.
How it saves time or tokens
Rayon's API requires minimal code changes: replace .iter() with .par_iter() or .into_iter() with .into_par_iter(). The library handles thread pool creation, work-stealing scheduling, and load balancing internally. You get multi-core performance without writing thread synchronization code or worrying about data races.
How to use
- Add Rayon to your project:
cargo add rayon. - Import the prelude:
use rayon::prelude::*. - Replace sequential iterator calls with their parallel equivalents (
.par_iter(),.par_sort(),.par_chunks()).
Example
use rayon::prelude::*;
fn main() {
// Sequential: single-core
let sum: i64 = (0..1_000_000).map(|x| x * x).sum();
// Parallel: all cores, one-line change
let par_sum: i64 = (0..1_000_000)
.into_par_iter()
.map(|x| x * x)
.sum();
// Parallel sort
let mut data = vec![5, 2, 8, 1, 9, 3];
data.par_sort();
// Parallel string processing
let results: Vec<String> = vec!["hello", "world", "rust"]
.par_iter()
.map(|s| s.to_uppercase())
.collect();
}
Related on TokRepo
- AI Tools for Coding -- explore coding tools and libraries for performance-critical development
- Featured Workflows -- browse top-rated developer tools and utilities
Common pitfalls
- Not all workloads benefit from parallelism; very short iterations may run slower due to thread pool overhead. Benchmark before and after.
- Rayon's global thread pool defaults to one thread per CPU core; customize with
rayon::ThreadPoolBuilderif you need to limit concurrency. - Parallel iterators require
Send + Syncbounds on the data; closures that capture non-thread-safe types will not compile.
Frequently Asked Questions
Rayon leverages Rust's ownership and borrowing system. The ParallelIterator trait requires that closures and data satisfy Send and Sync bounds, which the Rust compiler checks at compile time. If your code compiles, it is free of data races.
Rayon uses a work-stealing thread pool where idle threads take tasks from busy threads' queues. This provides automatic load balancing without manual task distribution, even when iteration elements have uneven processing times.
Yes. Use rayon::ThreadPoolBuilder to create custom thread pools with specific thread counts. You can also use pool.install() to run parallel operations on a specific pool instead of the global one.
Rayon is designed for CPU-bound parallelism, not async I/O. You can use Rayon within async code by spawning blocking tasks via tokio::task::spawn_blocking, but do not mix Rayon iterators with async/await directly.
Speedup depends on the workload. CPU-bound tasks with independent elements typically see near-linear scaling up to the number of cores. I/O-bound or synchronization-heavy workloads see diminishing returns.
Citations (3)
- Rayon GitHub— Rayon provides parallel iterators for Rust
- Rayon Docs— Rayon documentation and API reference
- Rust Book— Rust Send and Sync traits for thread safety
Related on TokRepo
Discussion
Related Assets
Supertest — HTTP Assertion Library for Node.js APIs
Supertest provides a high-level API for testing HTTP servers in Node.js with fluent assertion chaining.
PHPUnit — The Standard Testing Framework for PHP
PHPUnit is the de facto unit testing framework for PHP, providing assertions, mocks, and code coverage analysis.
GoogleTest — C++ Testing Framework by Google
GoogleTest is Google's open-source C++ testing and mocking framework used across thousands of production codebases.