Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
749 words
4 minutes
260201_fold_advantage
link
Here is a rearranged summary of the advantages of Rust’s fold function:
| Advantage | Description | Example |
|---|---|---|
| General-purpose | Can combine iterator elements using almost any custom operation. | fold(0, |acc, x| acc + x) |
| Clear intent | Expresses the idea of reducing many values into one result. | numbers.iter().fold(...) |
| Reduces mutable state | Avoids manually maintaining an external mutable accumulator variable. | let sum = ... instead of let mut sum = 0 |
| Flexible accumulator type | The accumulator can be a different type from the iterator’s elements. | Iterator<Item = i32> → (i32, i32) |
| Supports custom logic | Can implement operations that sum() or product() cannot express directly. | Counting, grouping, building structures |
| Composable with iterators | Works naturally with Rust’s iterator pipeline. | .filter(...).map(...).fold(...) |
| Functional programming style | Encourages immutable values and transformation-based programming. | let result = iter.fold(...) |
| Can replace loops | Many explicit accumulation loops can be expressed concisely with fold. | for loop → fold |
Simple comparison
| Imperative loop | fold |
|---|---|
let mut sum = 0; | let sum = ... |
for loop | Iterator |
Mutates sum | Produces a new accumulator |
| Explicit control flow | Declarative reduction |
| Good for complex control flow | Good 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()andproduct(). Usefold()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
foldis 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
foldoperation 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
foldwhen 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:
foldis 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/