Updated 'src/async/multiple_futures.md'.

This commit is contained in:
Hector PENG
2026-04-12 10:17:01 +08:00
parent 3240c5d08c
commit fa5fa2c297
2 changed files with 38 additions and 628 deletions

View File

@@ -1,4 +1,5 @@
use std::time::Duration;
use trpl::Either;
fn main() {
let fut = async {
@@ -17,3 +18,13 @@ fn main() {
trpl::block_on(fut);
}
async fn timeout<F: Future>(
future_to_try: F,
max_time: Duration,
) -> Result<F::Output, Duration> {
match trpl::select(future_to_try, trpl::sleep(max_time)).await {
Either::Left(output) => Ok(output),
Either::Right(_) => Err(max_time),
}
}