]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0434.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0434.md
CommitLineData
ba9703b0 1A variable used inside an inner function comes from a dynamic environment.
60c5eb7d
XL
2
3Erroneous code example:
4
5```compile_fail,E0434
6fn foo() {
7 let y = 5;
8 fn bar() -> u32 {
9 y // error: can't capture dynamic environment in a fn item; use the
10 // || { ... } closure form instead.
11 }
12}
13```
14
ba9703b0
XL
15Inner functions do not have access to their containing environment. To fix this
16error, you can replace the function with a closure:
60c5eb7d
XL
17
18```
19fn foo() {
20 let y = 5;
21 let bar = || {
22 y
23 };
24}
25```
26
ba9703b0 27Or replace the captured variable with a constant or a static item:
60c5eb7d
XL
28
29```
30fn foo() {
31 static mut X: u32 = 4;
32 const Y: u32 = 5;
33 fn bar() -> u32 {
34 unsafe {
35 X = 3;
36 }
37 Y
38 }
39}
40```