]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0373.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0373.md
1 A captured variable in a closure may not live long enough.
2
3 Erroneous code example:
4
5 ```compile_fail,E0373
6 fn foo() -> Box<Fn(u32) -> u32> {
7     let x = 0u32;
8     Box::new(|y| x + y)
9 }
10 ```
11
12 This error occurs when an attempt is made to use data captured by a closure,
13 when that data may no longer exist. It's most commonly seen when attempting to
14 return a closure as shown in the previous code example.
15
16 Notice that `x` is stack-allocated by `foo()`. By default, Rust captures
17 closed-over data by reference. This means that once `foo()` returns, `x` no
18 longer exists. An attempt to access `x` within the closure would thus be
19 unsafe.
20
21 Another situation where this might be encountered is when spawning threads:
22
23 ```compile_fail,E0373
24 fn foo() {
25     let x = 0u32;
26     let y = 1u32;
27
28     let thr = std::thread::spawn(|| {
29         x + y
30     });
31 }
32 ```
33
34 Since our new thread runs in parallel, the stack frame containing `x` and `y`
35 may well have disappeared by the time we try to use them. Even if we call
36 `thr.join()` within foo (which blocks until `thr` has completed, ensuring the
37 stack frame won't disappear), we will not succeed: the compiler cannot prove
38 that this behavior is safe, and so won't let us do it.
39
40 The solution to this problem is usually to switch to using a `move` closure.
41 This approach moves (or copies, where possible) data into the closure, rather
42 than taking references to it. For example:
43
44 ```
45 fn foo() -> Box<Fn(u32) -> u32> {
46     let x = 0u32;
47     Box::new(move |y| x + y)
48 }
49 ```
50
51 Now that the closure has its own copy of the data, there's no need to worry
52 about safety.
53
54 This error may also be encountered while using `async` blocks:
55
56 ```compile_fail,E0373,edition2018
57 use std::future::Future;
58
59 async fn f() {
60     let v = vec![1, 2, 3i32];
61     spawn(async { //~ ERROR E0373
62         println!("{:?}", v)
63     });
64 }
65
66 fn spawn<F: Future + Send + 'static>(future: F) {
67     unimplemented!()
68 }
69 ```
70
71 Similarly to closures, `async` blocks are not executed immediately and may
72 capture closed-over data by reference. For more information, see
73 https://rust-lang.github.io/async-book/03_async_await/01_chapter.html.