]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0733.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0733.md
1 An [`async`] function used recursion without boxing.
2
3 Erroneous code example:
4
5 ```edition2018,compile_fail,E0733
6 async fn foo(n: usize) {
7     if n > 0 {
8         foo(n - 1).await;
9     }
10 }
11 ```
12
13 To perform async recursion, the `async fn` needs to be desugared such that the
14 `Future` is explicit in the return type:
15
16 ```edition2018,compile_fail,E0720
17 use std::future::Future;
18 fn foo_desugared(n: usize) -> impl Future<Output = ()> {
19     async move {
20         if n > 0 {
21             foo_desugared(n - 1).await;
22         }
23     }
24 }
25 ```
26
27 Finally, the future is wrapped in a pinned box:
28
29 ```edition2018
30 use std::future::Future;
31 use std::pin::Pin;
32 fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> {
33     Box::pin(async move {
34         if n > 0 {
35             foo_recursive(n - 1).await;
36         }
37     })
38 }
39 ```
40
41 The `Box<...>` ensures that the result is of known size, and the pin is
42 required to keep it in the same place in memory.
43
44 [`async`]: https://doc.rust-lang.org/std/keyword.async.html