src/iterators/helpers.md
Iterator Helper MethodsIn addition to the next method that defines how an iterator behaves, the
Iterator trait provides 70+ helper methods that can be used to build
customized iterators.
# // Copyright 2024 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
fn main() {
let result: i32 = (1..=10) // Create a range from 1 to 10
.filter(|x| x % 2 == 0) // Keep only even numbers
.map(|x| x * x) // Square each number
.sum(); // Sum up all the squared numbers
println!("The sum of squares of even numbers from 1 to 10 is: {}", result);
}
The Iterator trait implements many common functional programming operations
over collections (e.g. map, filter, reduce, etc). This is the trait
where you can find all the documentation about them.
Many of these helper methods take the original iterator and produce a new iterator with different behavior. These are know as "iterator adapter methods".
Some methods, like sum and count, consume the iterator and pull all of the
elements out of it.
These methods are designed to be chained together so that it's easy to build a custom iterator that does exactly what you need.