]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0434.md
Update const_forget.rs
[rust.git] / src / librustc_error_codes / error_codes / E0434.md
1 This error indicates that a variable usage inside an inner function is invalid
2 because the variable comes from a dynamic environment. Inner functions do not
3 have access to their containing environment.
4
5 Erroneous code example:
6
7 ```compile_fail,E0434
8 fn foo() {
9     let y = 5;
10     fn bar() -> u32 {
11         y // error: can't capture dynamic environment in a fn item; use the
12           //        || { ... } closure form instead.
13     }
14 }
15 ```
16
17 Functions do not capture local variables. To fix this error, you can replace the
18 function with a closure:
19
20 ```
21 fn foo() {
22     let y = 5;
23     let bar = || {
24         y
25     };
26 }
27 ```
28
29 or replace the captured variable with a constant or a static item:
30
31 ```
32 fn foo() {
33     static mut X: u32 = 4;
34     const Y: u32 = 5;
35     fn bar() -> u32 {
36         unsafe {
37             X = 3;
38         }
39         Y
40     }
41 }
42 ```