mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
Updated 'src/async/multiple_futures.md'.
This commit is contained in:
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user