Lazy iterators — only compute when needed:
use std::Iter let nums = [1, 2, 3, 4, 5] let doubled = Iter::from(nums) .map(|x| x * 2) .collect() print(doubled) // [2, 4, 6, 8, 10] let evens = Iter::from(nums) .filter(|x| x % 2 == 0) .collect() print(evens) // [2, 4]
Goal: From [1..10], filter the evens and double each one. Result: [4, 8, 12, 16, 20].
[1..10]
[4, 8, 12, 16, 20]
Run the code to see the output
9 results