Rayon Parallel Iterator Fold

Parallelize CPU-bound aggregation with rayon par_iter, folding partitions independently and reducing partial results safely.

RustResilience

Code

snippet
use rayon::prelude::*;

fn word_frequencies(lines: &[String]) -> std::collections::HashMap<String, usize> {
    lines
        .par_iter()
        .flat_map(|line| line.split_whitespace())
        .fold(
            || std::collections::HashMap::new(),
            |mut acc, word| {
                *acc.entry(word.to_lowercase()).or_insert(0) += 1;
                acc
            },
        )
        .reduce(
            |mut a, b| {
                for (word, count) in b {
                    *a.entry(word).or_insert(0) += count;
                }
                a
            },
        )
}

fn checksum_parallel(bytes: &[u8]) -> u64 {
    bytes
        .par_chunks(1024 * 256)
        .map(|chunk| {
            // 每块独立校验:避免跨块状态
            chunk.iter().map(|&b| b as u64).sum::<u64>()
        )
        .sum()
}

fn main() {
    let corpus: Vec<String> = vec![
        "the quick brown fox".into(),
        "the lazy dog sleeps".into(),
        "brown fox jumps high".into(),
    ];

    let freqs = word_frequencies(&corpus);
    for (word, count) in freqs.iter().take(3) {
        println!("{word}: {count}");
    }

    let data = vec![7u8; 1024 * 1024];
    println!("checksum: {}", checksum_parallel(&data));

    let max_len = corpus
        .par_iter()
        .map(|line| line.len())
        .max()
        .unwrap_or(0);
    println!("longest line: {max_len}");
}