Skip to content
← Index | Lesson 1 of 5

map and filter

Iter.map and Iter.filter

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].

lesson-6-1.zolo
Program output

Run the code to see the output

Search Zolo

9 results

enespt-br