Skip to content
← Index | Lesson 5 of 5

from_fn and Fibonacci

Iter.from_fn for custom iterators

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

Search Zolo

9 results

enespt-br