Rayon Parallel Iterator Fold
Parallelize CPU-bound aggregation with rayon par_iter, folding partitions independently and reducing partial results safely.
Code
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}");
}Inside the snippet
The working surface is the functions word_frequencies, checksum_parallel and main — everything else is wiring.
Repetition is confined to for (word, count) in b — the rest of the file is straight-line.
Points of care
- Requires
rayon::prelude::as a package; the snippet will not run without it.
Using it
The entry-point signature is fn word_frequencies(lines: &[String]) -> std::collections::HashMap<String, usize>
Output lines are prefixed (checksum:, longest line:), making each step’s effect easy to spot.
main sequences word_frequencies and checksum_parallel; executing the file runs that flow once.
Key statements
for (word, count) in freqs.iter().take(3) {
println!("{word}: {count}");
}
chunk.iter().map(|&b| b as u64).sum::<u64>()
)
.sum()
}
.unwrap_or(0);
Interactive Tools for This Topic
HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.Kubernetes Resource CalculatorCalculate per-pod and cluster CPU and memory requests, limits, QoS class, overcommit ratios, headroom, and node fit estimates for Kubernetes workloads.
FAQ
- Which packages must be installed first?
- Yes. The imports resolve to `rayon::prelude::`, which the package manager must provide.
Report a problem with this exampleOpens a pre-filled issue on GitHub.