Update: this question has been answered in Is there any way to return a reference to a variable created in a function?
Also the confusing example that mentioned has been cleared in the new Rust book.
Based on the closure examples in the Rust book:
fn factory() -> &(Fn(i32) -> i32) {
let num = 5;
|x| x + num
}
let f = factory();
let answer = f(1);
assert_eq!(6, answer);
This example would have error:
error: missing lifetime specifier [E0106]
fn factory() -> &(Fn(i32) -> i32) {
^~~~~~~~~~~~~~~~~
I found this fn factory() -> &(Fn(i32) -> i32)
can be abstracted as
fn foo<'a>() -> &'a Someclosure.
I am trying to understand why a function takes no arguments would have a lifetime problem here.
Is that similar to the example given in the lifetime elision in Rust book:fn foo<'a>() -> &'a str
? This seems to be a valid example about how to use lifetime.