expression

Timeout around a directly owned future

Distinguish a future result from expiration. A timeout checks cooperative progress, not preemptive CPU interruption. After expiration the directly owned inner future is dropped when the wrapper is dropped. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 7 true Common mistake: Assuming a timeout interrupts synchronous work that never returns control.

Formal Syntax & Structure

rust syntaxrust
tokio::time::timeout(duration, future).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::time::{timeout, Duration};    let fast = timeout(Duration::from_secs(1), async { 7 }).await;    let slow = timeout(Duration::from_secs(1), std::future::pending::<()>()).await;    println!("{} {}", fast.unwrap(), slow.is_err());}
Rust Reference