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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1chunks lets you process a slice in fixed-size windows without manual index math.
- 2fold threads an accumulator through an iterator to build up state in one pass.
- 3entry with or_insert is the idiomatic way to accumulate into a HashMap by key.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/batched-aggregation-with-fold-in-rust-explained-rust-271b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.