futures_lite::stream

Function race

source
pub fn race<T, S1, S2>(stream1: S1, stream2: S2) -> Race<S1, S2>
where S1: Stream<Item = T>, S2: Stream<Item = T>,
Expand description

Merges two streams, with no preference for either stream when both are ready.

ยงExamples

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

assert_eq!(stream::race(once(1), pending()).next().await, Some(1));
assert_eq!(stream::race(pending(), once(2)).next().await, Some(2));

// One of the two stream is randomly chosen as the winner.
let res = stream::race(once(1), once(2)).next().await;