C+
Systems · View as Markdown

Async / await

An async fn returns Future[T], written as the bare T in its signature. await EXPR suspends the current future until another future resolves.

async fn inner() -> i32 { return 7; }

async fn main() -> i32 {
    let x: i32 = await inner();
    return x +% 1;
}

In 0.0.28 the compiler drives async fn main and async #[test] functions. The removed executor::block_on and executor::run entry points must not be used.

When synchronous code must consume a future, call its .wait() method or use the cancellable future::wait_or_cancel path. Detached work is submitted through executor::spawn_local; the reactor owns it through teardown.

Lifetime rules

Borrow-shaped parameters such as str, T[], and ref non-Copy values are rejected in an async signature with E0900 because a suspension can outlive the caller's frame. Pass owned values such as Text and Vec[T] instead.

Dropping a future destroys its suspended frame tree. Thread scopes request cancellation before joining, and the async/thread bridges, sockets, and platform reactors provide deadline-aware operations.

Reactor and cancellation

Timers and asynchronous socket operations yield the executor instead of blocking its thread. Cancellation is cooperative: a future observes the token at suspension points. For the complete model and the choice between threads, async work, channels, and generators, see Concurrency.

Runnable recipes are collected under Async / await.