1 A captured variable in a closure may not live long enough.
3 Erroneous code example:
6 fn foo() -> Box<Fn(u32) -> u32> {
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.
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
21 Another situation where this might be encountered is when spawning threads:
28 let thr = std::thread::spawn(|| {
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.
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:
45 fn foo() -> Box<Fn(u32) -> u32> {
47 Box::new(move |y| x + y)
51 Now that the closure has its own copy of the data, there's no need to worry
54 This error may also be encountered while using `async` blocks:
56 ```compile_fail,E0373,edition2018
57 use std::future::Future;
60 let v = vec![1, 2, 3i32];
61 spawn(async { //~ ERROR E0373
66 fn spawn<F: Future + Send + 'static>(future: F) {
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.