@@ -1206,6 +1206,28 @@ mod ref_keyword {}
1206
1206
/// Ok(())
1207
1207
/// }
1208
1208
/// ```
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
1209
1231
mod return_keyword { }
1210
1232
1211
1233
#[ doc( keyword = "self" ) ]
@@ -2399,6 +2421,39 @@ mod while_keyword {}
2399
2421
///
2400
2422
/// We have written an [async book] detailing `async`/`await` and trade-offs compared to using threads.
2401
2423
///
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
+ ///
2402
2457
/// ## Editions
2403
2458
///
2404
2459
/// `async` is a keyword from the 2018 edition onwards.
@@ -2408,6 +2463,11 @@ mod while_keyword {}
2408
2463
/// [`Future`]: future::Future
2409
2464
/// [`.await`]: ../std/keyword.await.html
2410
2465
/// [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
2411
2471
mod async_keyword { }
2412
2472
2413
2473
#[ doc( keyword = "await" ) ]
0 commit comments