I have a fairly simple program:
fn f<'a>() -> &'a i32 {
&1
}
fn main() {
println!("{}", f());
}
It doesn't compile (some of the output elided):
$ rustc test.rs
test.rs:2:6: 2:7 error: borrowed value does not live long enough
test.rs:2 &1
I understand why it fails.
- I don't know how to return a reference created inside the function scope. Is there way to do that?
- Why can't the lifetime be elided for a single return?
EDIT: I changed the title since it suggested returning boxed type would help which is not (see answers).