749 words
4 minutes
260201_fold_advantage

link#


Here is a rearranged summary of the advantages of Rust’s fold function:#

AdvantageDescriptionExample
General-purposeCan combine iterator elements using almost any custom operation.fold(0, |acc, x| acc + x)
Clear intentExpresses the idea of reducing many values into one result.numbers.iter().fold(...)
Reduces mutable stateAvoids manually maintaining an external mutable accumulator variable.let sum = ... instead of let mut sum = 0
Flexible accumulator typeThe accumulator can be a different type from the iterator’s elements.Iterator<Item = i32>(i32, i32)
Supports custom logicCan implement operations that sum() or product() cannot express directly.Counting, grouping, building structures
Composable with iteratorsWorks naturally with Rust’s iterator pipeline..filter(...).map(...).fold(...)
Functional programming styleEncourages immutable values and transformation-based programming.let result = iter.fold(...)
Can replace loopsMany explicit accumulation loops can be expressed concisely with fold.for loop → fold

Simple comparison#

Imperative loopfold
let mut sum = 0;let sum = ...
for loopIterator
Mutates sumProduces a new accumulator
Explicit control flowDeclarative reduction
Good for complex control flowGood for accumulation/reduction
  • For example:
// Imperative
let mut sum = 0;

for x in numbers {
    sum += x;
}
  • becomes:
// fold
let sum = numbers.iter().fold(0, |acc, &x| acc + x);
  • The key idea is:

fold takes a sequence of values and repeatedly combines them into a single accumulated result.

  • For simple cases, prefer specialized methods such as sum() and product(). Use fold() when you need custom accumulation logic.

In Rust, fold is useful when you want to combine all elements of an iterator into a single value.#

  • The basic form is:
iterator.fold(initial_value, |accumulator, item| {
    // return the next accumulator
})
  • For example:
let numbers = [1, 2, 3, 4];

let sum = numbers.iter().fold(0, |acc, &x| acc + x);

println!("{sum}"); // 10
  • This is conceptually equivalent to:
let mut sum = 0;

for x in numbers {
    sum += x;
}

Benefits of fold#

1. Expresses the intent clearly#

  • Instead of manually managing a mutable variable:
let mut sum = 0;

for x in numbers {
    sum += x;
}
  • you can say:
let sum = numbers.iter().fold(0, |acc, &x| acc + x);
  • The idea is:

Take all elements and fold them into one result.

2. Works with many different operations#

  • Addition:
let sum = numbers.iter().fold(0, |acc, &x| acc + x);
  • Multiplication:
let product = numbers.iter().fold(1, |acc, &x| acc * x);
  • Maximum:
let max = numbers.iter().fold(i32::MIN, |acc, &x| acc.max(x));
  • String construction:
let text = ["Hello", " ", "Rust"]
    .iter()
    .fold(String::new(), |mut acc, &x| {
        acc.push_str(x);
        acc
    });
  • So fold is a general-purpose accumulation mechanism.

3. Avoids external mutable state#

  • With an imperative loop:
let mut sum = 0;

for x in numbers {
    sum += x;
}
  • you have a mutable variable whose value changes during the loop.

  • With fold:

let sum = numbers.iter().fold(0, |acc, &x| acc + x);
  • the accumulator is managed by the fold operation itself.

  • This often makes functional-style code easier to reason about.

4. Can transform data into a completely different type#

  • The accumulator doesn’t have to be the same type as the elements.

  • For example, count positive numbers:

let numbers = [-2, 1, -5, 3, 4];

let positive_count = numbers.iter().fold(0, |count, &x| {
    if x > 0 {
        count + 1
    } else {
        count
    }
});

println!("{positive_count}"); // 3
  • Or calculate multiple results:
let numbers = [1, 2, 3, 4];

let (sum, count) = numbers.iter().fold((0, 0), |(sum, count), &x| {
    (sum + x, count + 1)
});

let average = sum as f64 / count as f64;
  • Here:
Iterator<Item = i32>

        │ fold

    (i32, i32)
  • The input and output types can be different.

fold vs sum and product#

  • For simple operations, Rust provides specialized methods:
// main.rs
let sum: i32 = numbers.iter().sum();
let product: i32 = numbers.iter().product();
  • These are generally preferable because they communicate the intent directly.

  • Use fold when you need a custom accumulation operation:

// main.rs
let result = numbers.iter().fold(initial_value, |acc, item| {
    // custom logic
});
  • So, in your original C++ example:
// main.cpp
std::accumulate(scores.cbegin(), scores.cend(), 0)
  • the closest Rust equivalent is:
// main.rs
scores.iter().fold(0, |acc, &score| acc + score)
  • But for this particular case, idiomatic Rust would usually be:
// main.rs
scores.iter().sum::<i32>()
  • In short: fold is valuable because it gives you a general, composable way to reduce an iterator to one result, while keeping the accumulation logic explicit and allowing the accumulator to have any type.
  • 간단히 말해서: 접기(fold)는 반복기를 하나의 결과로 줄일 수 있는 일반적이고 합성 가능한 방법을 제공하면서도 축적 논리를 명확하게 유지하고 축적기가 어떤 유형이든 가질 수 있게 해주기 때문에 가치가 있습니다.
260201_fold_advantage
https://younghakim7.github.io/blog/posts/260201_fold_advantage/
Author
YoungHa
Published at
2026-02-01