]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0515.md
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0515.md
1 Cannot return value that references local variable
2
3 Local variables, function parameters and temporaries are all dropped before the
4 end of the function body. So a reference to them cannot be returned.
5
6 Erroneous code example:
7
8 ```compile_fail,E0515
9 fn get_dangling_reference() -> &'static i32 {
10 let x = 0;
11 &x
12 }
13 ```
14
15 ```compile_fail,E0515
16 use std::slice::Iter;
17 fn get_dangling_iterator<'a>() -> Iter<'a, i32> {
18 let v = vec![1, 2, 3];
19 v.iter()
20 }
21 ```
22
23 Consider returning an owned value instead:
24
25 ```
26 use std::vec::IntoIter;
27
28 fn get_integer() -> i32 {
29 let x = 0;
30 x
31 }
32
33 fn get_owned_iterator() -> IntoIter<i32> {
34 let v = vec![1, 2, 3];
35 v.into_iter()
36 }
37 ```