fold accumulates values with a function:
fold
use std::Iter let nums = [1, 2, 3, 4, 5] // Sum let sum = Iter::from(nums).fold(0, |acc, x| acc + x) print(sum) // 15 // Product let product = Iter::from(nums).fold(1, |acc, x| acc * x) print(product) // 120
Goal: Use fold to find the largest number in [3, 7, 1, 9, 4, 6]. Result: 9.
[3, 7, 1, 9, 4, 6]
9
Run the code to see the output
9 results