pub async fn timeout<F, T>(
duration: Duration,
future: F,
) -> Result<T, TimeoutError>where
F: Future<Output = T>,Expand description
Execute a future with a timeout
Examples found in repository?
examples/timeout_demo.rs (line 19)
14fn main() {
15 let rt = Runtime::new();
16
17 rt.block_on(async {
18 // This will timeout
19 match timeout(Duration::from_secs(1), slow_operation()).await {
20 Ok(val) => println!("Slow operation completed: {}", val),
21 Err(_) => println!("Slow operation timed out!"),
22 }
23
24 // This will succeed
25 match timeout(Duration::from_secs(1), fast_operation()).await {
26 Ok(val) => println!("Fast operation completed: {}", val),
27 Err(_) => println!("Fast operation timed out!"),
28 }
29 });
30}