use rayon::prelude::*; use sha2::{Digest, Sha256}; const N: usize = 10; const SIZE: usize = 128 * 1024; fn main() { println!("sequential"); for checksum in Blobs::new(N).map(|x| sha256(&x)) { println!("{}", checksum); } println!("rayon"); Blobs::new(N) .par_bridge() .map(|x| sha256(&x)) .for_each(|x| println!("{}", x)); } struct Blobs { n: usize, } impl Blobs { fn new(n: usize) -> Self { Self { n } } } struct Blob { data: [u8; SIZE], } impl Blob { fn new() -> Self { Self { data: [0; SIZE] } } } impl Iterator for Blobs { type Item = Blob; fn next(&mut self) -> Option { if self.n > 0 { self.n -= 1; Some(Blob::new()) } else { None } } } fn sha256(blob: &Blob) -> String { let mut hasher = Sha256::new(); hasher.update(&blob.data); let hash = hasher.finalize(); format!("{:x}", hash) }