How to Use Rayon for Easy Parallelism in Rust

Use Rayon's par_iter() method to automatically parallelize collection operations across all CPU cores in Rust.

Rayon is a data-parallelism library that automatically splits work across all available CPU cores using the par_iter() method on collections. Add rayon to your Cargo.toml, import the prelude, and call par_iter() on your collection to run operations in parallel.

[dependencies]
rayon = "1.10"
use rayon::prelude::*;

fn main() {
    let v = vec![1, 2, 3, 4, 5];
    let sum: i32 = v.par_iter().sum();
    println!("Sum: {}", sum);
}