]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_error_codes/src/error_codes/E0515.md
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0515.md
1 A reference to a local variable was returned.
2
3 Erroneous code example:
4
5 ```compile_fail,E0515
6 fn get_dangling_reference() -> &'static i32 {
7 let x = 0;
8 &x
9 }
10 ```
11
12 ```compile_fail,E0515
13 use std::slice::Iter;
14 fn get_dangling_iterator<'a>() -> Iter<'a, i32> {
15 let v = vec![1, 2, 3];
16 v.iter()
17 }
18 ```
19
20 Local variables, function parameters and temporaries are all dropped before the
21 end of the function body. So a reference to them cannot be returned.
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 ```