Threading
rayon is a Rust library for parallelizing work. It uses the typesystem to provide a safe API.
#![allow(unused_imports)] extern crate rayon; use std::sync::atomic::{AtomicI32, Ordering}; use std::iter::FromIterator; use rayon::prelude::*; fn main() { // Create a list of integers from 0 to 100 let range = Vec::from_iter(0..100); let mut total = 0; range.iter().for_each(|&n| { total += n; }); println!("{}", total); }
Note the difference in the signatures of the two for_each methods
Fn and FnMut are traits describing closures. FnMut closures need mutable access to their environment, whereas Fn closures only need shared access and so are safe to run concurrently.