logo
pub fn interval(dur: Duration) -> Interval
Available on unstable only.
Expand description

Creates a new stream that yields at a set interval.

The stream first yields after dur, and continues to yield every dur after that. The stream accounts for time elapsed between calls, and will adjust accordingly to prevent time skews.

Each interval may be slightly longer than the specified duration, but never less.

Note that intervals are not intended for high resolution timers, but rather they will likely fire some granularity after the exact instant that they’re otherwise indicated to fire at.

See also: task::sleep.

Examples

Basic example:

use async_std::prelude::*;
use async_std::stream;
use std::time::Duration;

let mut interval = stream::interval(Duration::from_secs(4));
while let Some(_) = interval.next().await {
    println!("prints every four seconds");
}