Skip to content
Course map | Lesson 1 of 5

Iterators · 6.1

map and filter

All lessons are open — jump whenever you need.

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

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br