rust 37 lines · 9 steps

Batched aggregation with fold in Rust

Summing amounts per region in fixed-size batches using chunks, fold, and a merge step.

Explained by highlit
1use std::collections::HashMap;
2 
3#[derive(Debug, Clone)]
4struct Record {
5 id: u64,
6 region: String,
7 amount: f64,
8}
9 
10fn aggregate_by_region(records: &[Record], batch_size: usize) -> HashMap<String, f64> {
11 let mut totals: HashMap<String, f64> = HashMap::new();
12 
13 for (batch_index, batch) in records.chunks(batch_size).enumerate() {
14 let (partial, count) = batch.iter().fold(
15 (HashMap::<String, f64>::new(), 0usize),
16 |(mut acc, n), record| {
17 *acc.entry(record.region.clone()).or_insert(0.0) += record.amount;
18 (acc, n + 1)
19 },
20 );
21 
22 tracing::debug!(batch = batch_index, processed = count, "flushed batch");
23 
24 for (region, sum) in partial {
25 *totals.entry(region).or_insert(0.0) += sum;
26 }
27 }
28 
29 totals
30}
31 
32fn largest_batches(records: &[Record], batch_size: usize) -> Vec<f64> {
33 records
34 .chunks(batch_size)
35 .map(|batch| batch.iter().map(|r| r.amount).sum())
36 .collect()
37}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1chunks lets you process a slice in fixed-size windows without manual index math.
  2. 2fold threads an accumulator through an iterator to build up state in one pass.
  3. 3entry with or_insert is the idiomatic way to accumulate into a HashMap by key.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Batched aggregation with fold in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code