rayon
Rayon: A data parallelism library for Rust
Rayon is a Rust library that lets you run code in parallel across CPU cores by changing a single word in your iterator calls, while Rust's type system prevents data race bugs at compile time.
Rayon is a library for the Rust programming language that makes it easier to run code in parallel across multiple CPU cores. The core idea is that you can take code that processes a list of items one at a time and, with a small change, make it process many items at the same time. In many cases the change is as simple as replacing one word in your code: where you previously wrote something like "iter", you write "par_iter" instead, and Rayon handles the rest.
The library is designed to avoid a common category of bugs that plague parallel programs. When multiple parts of a program run at the same time and try to read and write the same data without coordination, the results can be unpredictable and hard to reproduce. Rayon's design works with Rust's type system to prevent this class of problem at compile time. If your code compiles with Rayon, the parallel version is expected to produce the same results as the original sequential version, with a possible exception when your code has side effects that depend on order, such as writing to a file.
Beyond the simple parallel iterator pattern, Rayon provides lower-level tools for splitting work manually and controlling how tasks are grouped. It also lets you create separate thread pools instead of using the shared default one, which can matter when you need to isolate work or control resource usage more carefully.
Rayon works in WebAssembly environments as well. By default it falls back to single-threaded behavior there, so existing code compiles without changes. If you need real multithreading in a WebAssembly context, the README points to a separate adapter project that enables it.
The project is open source and dual-licensed under the MIT and Apache 2.0 licenses. A demo directory in the repository includes runnable examples, including a visualization of a physics simulation where you can switch between sequential and parallel execution to observe the difference in speed.
Where it fits
- Speed up data processing pipelines by switching from sequential to parallel iterators with a one-word change.
- Build multithreaded simulations or batch algorithms without risking unpredictable data race bugs.
- Create isolated thread pools to control how much CPU a specific part of your app can use.