macro_rules! timeout {
($duration:expr, $future:expr) => { ... };
($cx:expr, $duration:expr, $future:expr) => { ... };
}Expand description
Runs a future with a timeout.
This macro races the provided future against a sleep, returning the result if it completes in time, or an error if it times out.
§Semantics
ⓘ
let result = timeout!(Duration::from_secs(5), operation).await;
match result {
Ok(value) => println!("Completed: {:?}", value),
Err(Elapsed) => println!("Timed out"),
}§Cancellation Behavior
When timeout fires:
- Main future is cancelled
- Cancellation follows standard protocol (drain + finalize)
timeout!returns after main future is fully drained
When main future completes:
- Sleep is cancelled
timeout!returns immediately (sleep cleanup is fast)