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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 82947385-3745-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
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.
常见问题
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.
引用来源 (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
讨论
相关资产
Polars — Blazingly Fast DataFrame Library in Rust
Polars is an extremely fast DataFrame library written in Rust with bindings for Python, Node.js, and R. Uses Apache Arrow columnar format, lazy evaluation, and multi-threaded query execution. The modern alternative to pandas for data engineering and analytics.
Bevy — Data-Driven Game Engine Built in Rust
Bevy is a refreshingly simple data-driven game engine built in Rust. Uses an Entity Component System (ECS), hot reloading, and a modular plugin architecture. The most popular Rust game engine and a new paradigm for game development.
pandas — Powerful Data Analysis and Manipulation for Python
pandas is the essential data analysis library for Python. It provides DataFrame and Series data structures for efficient manipulation of tabular data, time series, and structured datasets with an expressive API for filtering, grouping, joining, and reshaping.
TanStack Query — Async State & Data Fetching for the Web
TanStack Query (formerly React Query) is a powerful asynchronous state management library for TS/JS that handles server-state, caching, background updates, and data synchronization across React, Solid, Svelte, and Vue.