Skip to content

Clarify async block behaviour #139608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions library/std/src/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,28 @@ mod ref_keyword {}
/// Ok(())
/// }
/// ```
///
/// Within [closures] and [`async`] blocks, `return` returns a value from within the closure or
/// `async` block, not from the parent function:
///
/// ```rust
/// fn foo() -> i32 {
/// let closure = || {
/// return 5;
/// };
///
/// let future = async {
/// return 10;
/// };
///
/// return 15;
/// }
///
/// assert_eq!(foo(), 15);
/// ```
///
/// [closures]: ../book/ch13-01-closures.html
/// [`async`]: ../std/keyword.async.html
mod return_keyword {}

#[doc(keyword = "self")]
Expand Down Expand Up @@ -2388,6 +2410,39 @@ mod while_keyword {}
///
/// We have written an [async book] detailing `async`/`await` and trade-offs compared to using threads.
///
/// ## Control Flow
/// [`return`] statements and [`?`][try operator] operators within `async` blocks do not cause
/// a return from the parent function; rather, they cause the `Future` returned by the block to
/// return with that value.
///
/// For example, the following Rust function will return `5`, causing `x` to take the [`!` type][never type]:
/// ```rust
/// #[expect(unused_variables)]
/// fn example() -> i32 {
/// let x = {
/// return 5;
/// };
/// }
/// ```
/// In contrast, the following asynchronous function assigns a `Future<Output = i32>` to `x`, and
/// only returns `5` when `x` is `.await`ed:
/// ```rust
/// async fn example() -> i32 {
/// let x = async {
/// return 5;
/// };
///
/// x.await
/// }
/// ```
/// Code using `?` behaves similarly - it causes the `async` block to return a [`Result`] without
/// affecting the parent function.
///
/// Note that you cannot use `break` or `continue` from within an `async` block to affect the
/// control flow of a loop in the parent function.
///
/// Control flow in `async` blocks is documented further in the [async book][async book blocks].
///
/// ## Editions
///
/// `async` is a keyword from the 2018 edition onwards.
Expand All @@ -2397,6 +2452,11 @@ mod while_keyword {}
/// [`Future`]: future::Future
/// [`.await`]: ../std/keyword.await.html
/// [async book]: https://rust-lang.github.io/async-book/
/// [`return`]: ../std/keyword.return.html
/// [try operator]: ../reference/expressions/operator-expr.html#r-expr.try
/// [never type]: ../reference/types/never.html
/// [`Result`]: result::Result
/// [async book blocks]: https://rust-lang.github.io/async-book/part-guide/more-async-await.html#async-blocks
mod async_keyword {}

#[doc(keyword = "await")]
Expand Down
Loading