Function futures_lite::stream::unfold

source ·
pub fn unfold<T, F, Fut, Item>(seed: T, f: F) -> Unfold<T, F, Fut>
where F: FnMut(T) -> Fut, Fut: Future<Output = Option<(Item, T)>>,
Expand description

Creates a stream from a seed value and an async closure operating on it.

Examples

use futures_lite::stream::{self, StreamExt};

let s = stream::unfold(0, |mut n| async move {
    if n < 2 {
        let m = n + 1;
        Some((n, m))
    } else {
        None
    }
});

let v: Vec<i32> = s.collect().await;
assert_eq!(v, [0, 1]);