logo
pub fn repeat_with<T, F>(repeater: F) -> RepeatWith<F> where
    F: FnMut() -> T, 
Expand description

Creates a new stream that repeats elements of type A endlessly by applying the provided closure.

Examples

Basic usage:

use async_std::prelude::*;
use async_std::stream;

let s = stream::repeat_with(|| 1);

pin_utils::pin_mut!(s);

assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(1));

Going finite:

use async_std::prelude::*;
use async_std::stream;

let mut n = 1;
let s = stream::repeat_with(|| {
    let item = n;
    n *= 2;
    item
})
.take(4);

pin_utils::pin_mut!(s);

assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(4));
assert_eq!(s.next().await, Some(8));
assert_eq!(s.next().await, None);