fold carries an accumulator across the sequence, combining each item into a single result.

Program

Play the program to multiply every number into a running product.

fold_sum.rs
fn main() {
    let nums = vec![2, 4, 6];
    let product = nums.iter().fold(1, |acc, &n| acc * n);
    println!("{product}");
}
fold `fold(init, f)` starts from `init` and folds each item in.
accumulator `acc` carries state between steps.
reduce The whole sequence collapses to one value.