expression

Timeout waiting for a task is not task cancellation

Borrow the join handle during the timed wait. On expiry, the caller still owns the handle and can decide whether to cancel and observe termination. Passing an owned handle instead can detach the spawned task on drop. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true Common mistake: Dropping a join handle and expecting the spawned task to stop.

Formal Syntax & Structure

rust syntaxrust
timeout(duration, &mut handle); handle.abort(); handle.await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::time::{timeout, Duration};    let mut task = tokio::spawn(std::future::pending::<()>());    if timeout(Duration::from_secs(1), &mut task).await.is_err() {        task.abort();        println!("{}", task.await.unwrap_err().is_cancelled());    }}
Rust Reference