0

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.

Community
  • 1
  • 1
enaJ
  • 1,565
  • 5
  • 16
  • 29
  • 1
    Note that the examples you are looking are just examples of **syntax**; they aren't necessarily *valid* or *useful*. – Shepmaster Dec 04 '16 at 23:30
  • I see. Thanks for pointing me to the right direction. The other related post answers everything! – enaJ Dec 04 '16 at 23:34
  • 1
    You may wish to review [the new version of the book](http://rust-lang.github.io/book/ch10-03-lifetime-syntax.html) which removes that confusing line. – Shepmaster Dec 05 '16 at 00:03

0 Answers0