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 axum::{ body::{Body, Bytes}, extract::Request, http::StatusCode,
Logging request and response sizes in Axum
middleware
http
streaming-bodies
Advanced
8 steps
ruby
class BackfillUsersFullName < ActiveRecord::Migration[7.1] disable_ddl_transaction! BATCH_SIZE = 5_000
How to backfill a column safely in Rails
migrations
batching
backfill
Intermediate
7 steps
rust
use std::borrow::Cow; pub fn escape_html(input: &str) -> Cow<'_, str> { let needs_escape = input
Zero-copy HTML escaping with Cow in Rust
clone-on-write
zero-copy
string-escaping
Intermediate
6 steps
rust
use once_cell::sync::Lazy; use regex::Regex; static CREDIT_CARD: Lazy<Regex> = Lazy::new(|| {
Redacting sensitive data from logs in Rust
regex
lazy-initialization
checksum-validation
Intermediate
9 steps
rust
use axum::{ extract::{FromRequestParts, Query}, http::{request::Parts, StatusCode}, };
Building a custom Axum extractor for query filters
extractors
query-parsing
enums
Intermediate
8 steps
rust
use std::sync::Arc; use axum::{ extract::{Multipart, State}, http::StatusCode,
Throttling file uploads in Axum with a Semaphore
concurrency
backpressure
multipart
Advanced
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.