]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0733.md
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0733.md
1 Recursion in an `async fn` requires boxing. For example, this will not compile:
2
3 ```edition2018,compile_fail,E0733
4 async fn foo(n: usize) {
5 if n > 0 {
6 foo(n - 1).await;
7 }
8 }
9 ```
10
11 To achieve async recursion, the `async fn` needs to be desugared
12 such that the `Future` is explicit in the return type:
13
14 ```edition2018,compile_fail,E0720
15 use std::future::Future;
16 fn foo_desugared(n: usize) -> impl Future<Output = ()> {
17 async move {
18 if n > 0 {
19 foo_desugared(n - 1).await;
20 }
21 }
22 }
23 ```
24
25 Finally, the future is wrapped in a pinned box:
26
27 ```edition2018
28 use std::future::Future;
29 use std::pin::Pin;
30 fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> {
31 Box::pin(async move {
32 if n > 0 {
33 foo_recursive(n - 1).await;
34 }
35 })
36 }
37 ```
38
39 The `Box<...>` ensures that the result is of known size,
40 and the pin is required to keep it in the same place in memory.