Why the example can't run?


#1
fn factory() -> (Fn(i32) -> i32) {
    let num = 5;
    |x| x + num
}

fn main() {
    let f = factory();
    let answer = f(1);
    assert_eq!(6, answer);
}

I don’t know why, It just get a compile error, use rust 1.4 stable.


#2

This example seems pretty outdated. In Rust 1.x it would look like

fn factory() -> Box<Fn(i32) -> i32> {
    let num = 5;
    Box::new(move |x| x + num)
}

fn main() {
    let f = factory();
    let answer = f(1);
    assert_eq!(6, answer);
}

See http://smallcultfollowing.com/babysteps/blog/2014/11/26/purging-proc/ and http://huonw.github.io/blog/2015/05/finding-closure-in-rust/.