skip to content

In Rust, when do you reach for `Iterator::fold` instead of `sum` or `count`, and why can `.sum::<u16>()` panic in a debug build?

level: middleimportance: should knowfreq 55%

answer

  1. accumulator of any type
  2. Sum trait picks the output
  3. overflow checks in debug
  4. widen before summing

basics

~20 s

sum and count produce one number: sum for the type you name, count as a usize. fold carries an accumulator of any type, such as per-service totals in a map. sum panics on overflow when overflow checks are on, as in debug builds.

solid answer

~40 s

`count` returns a `usize` and calls `next` until `None`, so every closure in the chain still runs. `sum::<S>()` adds items through the `Sum` trait; `S` is a generic return type, so it needs an annotation or turbofish just like `collect`, and it returns zero for an empty iterator. `fold(init, f)` is the general form: it threads an accumulator of any type through `f` and returns it, which is what you want for per-service totals in a `HashMap`, a min-and-max pair, or a built `String`. On overflow, std documents that `sum` on a primitive integer panics when overflow checks are enabled — they are on in a default debug build, so `sum::<u16>()` over large codes panics with `attempt to add with overflow`; a default release build wraps silently instead. Widen first: `map(u32::from).sum::<u32>()`.

code

rust · 22 lines
rust
use std::collections::HashMap;

fn main() {
    let lines = ["ERROR auth 500", "ERROR web 503", "INFO web 200", "ERROR auth 502"];
    let errors = || {
        lines.iter().filter(|l| l.starts_with("ERROR")).map(|l| {
            let mut f = l.split(' ').skip(1);
            (f.next().unwrap(), f.next().unwrap().parse::<u16>().unwrap())
        })
    };

    let n = errors().count();
    let code_sum: u32 = errors().map(|(_, c)| u32::from(c)).sum();
    let per_service = errors().fold(HashMap::new(), |mut acc, (svc, _)| {
        *acc.entry(svc).or_insert(0u32) += 1;
        acc
    });
    println!("{n} {code_sum} auth={} web={}", per_service["auth"], per_service["web"]);

    let narrow: u16 = errors().map(|(_, c)| c * 50).sum(); // overflows u16
    println!("{narrow}");
}

go deeper

for a junior

Know that sum and count return a single number and fold builds any accumulator, and that sum needs its result type named.

for a middle

Explain the Sum bound and E0283, fold's left-to-right accumulator, and the debug-panic versus release-wrap overflow behaviour of sum on narrow integers.

for a senior

In review, widen narrow integer types before summing, flag counts that silently run expensive closures, and prefer one fold over several passes when the data is large or streamed.

for a principal

Decide where overflow must be detected rather than prevented, and whether release builds should keep overflow checks on for code that sums untrusted input.

## Three consumers, three jobs All three are **consumers**: they pull every item from the chain and produce one value. They differ in what that value can be. | Consumer | Signature, simplified | Result type | Empty iterator gives | |---|---|---|---| | `count()` | `fn count(self) -> usize` | always `usize` | `0` | | `sum::<S>()` | `fn sum<S: Sum<Self::Item>>(self) -> S` | the `S` you name | the zero of `S` | | `fold(init, f)` | `fn fold<B>(self, init: B, f: impl FnMut(B, Item) -> B) -> B` | any `B` | `init`, unchanged | | `reduce(f)` | `fn reduce(self, f: impl FnMut(Item, Item) -> Item) -> Option<Item>` | `Option` of the item type | `None` | `count` is documented to call `next` until `None` and count the `Some`s, so behind a `map` every closure still runs; only some sources, such as a slice iterator, override it to answer without walking. In std, `count` and `for_each` are themselves written on top of `fold`. ## `sum`: the output type is yours to choose `sum` is generic over its return type, bounded by the **`Sum`** trait. `Sum<A>` is implemented for the integer and float types (for both `A` and `&A`), and for `Option` and `Result` wrappers. Because `S` only appears in the return type, this fails exactly like an unannotated `collect`: ```plaintext error[E0283]: type annotations needed = note: the type must implement `Sum<u32>` help: consider giving `total` an explicit type ``` The fix is the same: `let total: u32 = ...sum();` or `.sum::<u32>()`. ## `fold`: an accumulator of any shape `fold(init, f)` starts from `init` and replaces the accumulator with `f(acc, item)` for each item, left to right. Because `B` is unrelated to the item type, `fold` covers everything `sum` cannot: - per-key totals: `fold(HashMap::new(), |mut m, (svc, _)| { *m.entry(svc).or_insert(0u32) += 1; m })`; - several statistics in one pass: a `(min, max, total)` tuple; - building a value that is not a number, such as a `String`. The map is moved into the closure and moved back out on every step, so there is no copying. A plain `for` loop with `entry` is equally idiomatic; pick whichever reads better (the Entry API itself is covered with HashMap). `reduce` is the variant with no initial value: it uses the first item as the start and returns `None` for an empty iterator. ## Overflow: debug panics, release wraps std's documentation for `sum` says that when a primitive integer type is returned, the method will panic if the computation overflows and overflow checks are enabled. The Reference requires that panic when debug assertions are on, and leaves other builds free to panic or wrap with two's-complement results. With default profiles: 1. a debug build has overflow checks on, so `[500u16, 503, 502].iter().map(|c| c * 50).sum::<u16>()` — 75,250 in exact arithmetic — panics with `attempt to add with overflow`; 2. a default release build has them off, so the same code returns a silently wrapped `9714`; 3. neither is undefined behaviour; the release result is just wrong. The defensive habits: - **widen before summing**: `map(u32::from).sum::<u32>()` or `u64` for byte counts; - use `try_fold` with `checked_add` when overflow must be detected rather than avoided; - remember `count` itself returns `usize`, which is wide enough for any in-memory collection. (The `overflow-checks` profile setting is covered with Cargo build profiles.) ## Choosing in practice - One number of the item's kind: `sum` with an annotated, wide enough type. - How many items passed the filters: `count`. - Anything else — grouped totals, several stats, a built value: `fold`, or a `for` loop when the body has early returns or multiple branches. - Possible failure partway: `try_fold`, which can stop early instead of running to the end. - The largest or smallest item: `max`, `min`, `max_by_key` — they return `Option`, because an empty iterator has no maximum, unlike `sum`, which returns zero. - A product: `product::<S>()`, which works like `sum` through the `Product` trait, returns one for an empty iterator, and overflows just as readily. One pass matters when the source is a stream: a log read line by line can be consumed only once, so computing a count, a total and a per-service map means either one `fold` with a tuple accumulator or collecting the lines first.

  • Why does `count()` on `iter().map(expensive)` still call `expensive` for every item?
    `count` asks for every item by calling `next` until `None`, and `Map` can only produce an item by applying its closure. The closure's result is then discarded, but the work and any side effects already happened. If you only need the length of the source, count before the `map` or ask the collection for its length.
  • When is `reduce` a better fit than `fold`?
    When the accumulator has the item's own type and there is no natural starting value, such as the longest line or the latest timestamp. `reduce` starts from the first item and returns `Option`, giving `None` for an empty iterator, so you never invent a fake initial value. If the result type differs from the item type, you need `fold`.

  • sum returns i32 unless you say otherwise
  • Integer overflow in sum is undefined behaviour in release builds
  • fold can only produce a value of the iterator's item type
  • count is a constant-time length lookup that skips the closures
  • sum on an empty iterator panics because there is nothing to add