Function smol::stream::try_unfold
source · [−]pub fn try_unfold<T, E, F, Fut, Item>(init: T, f: F) -> TryUnfold<T, F, Fut> where
F: FnMut(T) -> Fut,
Fut: Future<Output = Result<Option<(Item, T)>, E>>,
Expand description
Creates a stream from a seed value and a fallible async closure operating on it.
Examples
use futures_lite::stream::{self, StreamExt};
let s = stream::try_unfold(0, |mut n| async move {
if n < 2 {
let m = n + 1;
Ok(Some((n, m)))
} else {
std::io::Result::Ok(None)
}
});
let v: Vec<i32> = s.try_collect().await?;
assert_eq!(v, [0, 1]);