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 std::env; use std::time::Duration; #[derive(Debug, Clone)]
Loading typed config from environment variables in Rust
configuration
error-handling
generics
Intermediate
7 steps
rust
use std::time::Duration; use tokio::time::sleep; #[derive(Debug)]
Async retry with exponential backoff in Rust
retry
exponential-backoff
async
Intermediate
8 steps
rust
use std::fs; use std::io; use std::path::{Path, PathBuf};
Recursive file search by extension in Rust
recursion
filesystem
error-handling
Intermediate
8 steps
rust
use axum::{ extract::ws::{Message, WebSocket, WebSocketUpgrade}, response::IntoResponse, };
How an Axum WebSocket echo server works
websockets
async streams
protocol handling
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, http::StatusCode, response::{IntoResponse, Response},
Typed error handling in an Axum handler
error-handling
extractors
validation
Intermediate
9 steps
rust
use std::collections::BinaryHeap; use std::cmp::Reverse; pub fn top_k<T: Ord + Clone>(items: &[T], k: usize) -> Vec<T> {
Top-K selection with a bounded min-heap in Rust
heap
top-k
generics
Intermediate
8 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.