Skip to content
Course map | Lesson 5 of 5

Iterators · 6.5

from_fn and Fibonacci

All lessons are open — jump whenever you need.

Iter.from_fn creates an iterator from a closure:

fn counter(start: int) {
    let mut n = start
    return Iter.from_fn(|| {
        let val = n
        n = n + 1
        return val
    })
}

let c = counter(10) |> Iter.take(4) |> Iter.collect()
print(c) // [10, 11, 12, 13]

Goal: Create a Fibonacci iterator using Iter.from_fn and take the first 8 terms.

lesson-6-5.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