]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0597.md
Auto merge of #66396 - smmalis37:pythontest, r=alexcrichton
[rust.git] / src / librustc_error_codes / error_codes / E0597.md
1 This error occurs because a value was dropped while it was still borrowed
2
3 Erroneous code example:
4
5 ```compile_fail,E0597
6 struct Foo<'a> {
7     x: Option<&'a u32>,
8 }
9
10 let mut x = Foo { x: None };
11 {
12     let y = 0;
13     x.x = Some(&y); // error: `y` does not live long enough
14 }
15 println!("{:?}", x.x);
16 ```
17
18 In here, `y` is dropped at the end of the inner scope, but it is borrowed by
19 `x` until the `println`. To fix the previous example, just remove the scope
20 so that `y` isn't dropped until after the println
21
22 ```
23 struct Foo<'a> {
24     x: Option<&'a u32>,
25 }
26
27 let mut x = Foo { x: None };
28
29 let y = 0;
30 x.x = Some(&y);
31
32 println!("{:?}", x.x);
33 ```