Skip to content

Commit 192fbcc

Browse files
authored
Rollup merge of #139608 - Lynnesbian:improve-async-block-docs, r=ibraheemdev
Clarify `async` block behaviour Adds some documentation for control flow behaviour pertaining to `return` and `?` within `async` blocks. Fixes (or at least improves) #101444. r? rust-lang/docs
2 parents 8ffdb00 + 360012f commit 192fbcc

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

library/std/src/keyword_docs.rs

+60
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,28 @@ mod ref_keyword {}
12061206
/// Ok(())
12071207
/// }
12081208
/// ```
1209+
///
1210+
/// Within [closures] and [`async`] blocks, `return` returns a value from within the closure or
1211+
/// `async` block, not from the parent function:
1212+
///
1213+
/// ```rust
1214+
/// fn foo() -> i32 {
1215+
/// let closure = || {
1216+
/// return 5;
1217+
/// };
1218+
///
1219+
/// let future = async {
1220+
/// return 10;
1221+
/// };
1222+
///
1223+
/// return 15;
1224+
/// }
1225+
///
1226+
/// assert_eq!(foo(), 15);
1227+
/// ```
1228+
///
1229+
/// [closures]: ../book/ch13-01-closures.html
1230+
/// [`async`]: ../std/keyword.async.html
12091231
mod return_keyword {}
12101232

12111233
#[doc(keyword = "self")]
@@ -2399,6 +2421,39 @@ mod while_keyword {}
23992421
///
24002422
/// We have written an [async book] detailing `async`/`await` and trade-offs compared to using threads.
24012423
///
2424+
/// ## Control Flow
2425+
/// [`return`] statements and [`?`][try operator] operators within `async` blocks do not cause
2426+
/// a return from the parent function; rather, they cause the `Future` returned by the block to
2427+
/// return with that value.
2428+
///
2429+
/// For example, the following Rust function will return `5`, causing `x` to take the [`!` type][never type]:
2430+
/// ```rust
2431+
/// #[expect(unused_variables)]
2432+
/// fn example() -> i32 {
2433+
/// let x = {
2434+
/// return 5;
2435+
/// };
2436+
/// }
2437+
/// ```
2438+
/// In contrast, the following asynchronous function assigns a `Future<Output = i32>` to `x`, and
2439+
/// only returns `5` when `x` is `.await`ed:
2440+
/// ```rust
2441+
/// async fn example() -> i32 {
2442+
/// let x = async {
2443+
/// return 5;
2444+
/// };
2445+
///
2446+
/// x.await
2447+
/// }
2448+
/// ```
2449+
/// Code using `?` behaves similarly - it causes the `async` block to return a [`Result`] without
2450+
/// affecting the parent function.
2451+
///
2452+
/// Note that you cannot use `break` or `continue` from within an `async` block to affect the
2453+
/// control flow of a loop in the parent function.
2454+
///
2455+
/// Control flow in `async` blocks is documented further in the [async book][async book blocks].
2456+
///
24022457
/// ## Editions
24032458
///
24042459
/// `async` is a keyword from the 2018 edition onwards.
@@ -2408,6 +2463,11 @@ mod while_keyword {}
24082463
/// [`Future`]: future::Future
24092464
/// [`.await`]: ../std/keyword.await.html
24102465
/// [async book]: https://rust-lang.github.io/async-book/
2466+
/// [`return`]: ../std/keyword.return.html
2467+
/// [try operator]: ../reference/expressions/operator-expr.html#r-expr.try
2468+
/// [never type]: ../reference/types/never.html
2469+
/// [`Result`]: result::Result
2470+
/// [async book blocks]: https://rust-lang.github.io/async-book/part-guide/more-async-await.html#async-blocks
24112471
mod async_keyword {}
24122472

24132473
#[doc(keyword = "await")]

0 commit comments

Comments
 (0)