闭包=匿名函数=lambda,函数式编程的东西。

//非匿名函数解决法
fn main() {
    let fib = |x| {
        fn fib_impl(x: i64) -> i64 {
            match x < 2 {
                true => x,
                _ => fib_impl(x - 2) + fib_impl(x - 1),
            }
        }
        fib_impl(x)
    };
    let val = fib(10);
    println!("recur value :{}", val);
}
//匿名函数解决法
fn main() {
    let fib = |n| {
        struct Fib<'s> { f: &'s dyn Fn(&Fib, u32) -> u32 }
        let fib = Fib {
            f: &|fib, n| match n {
                0 | 1 => n,
                _ => (fib.f)(fib, n - 1) + (fib.f)(fib, n - 2),
            }
        };
        (fib.f)(&fib, n)
    };
    println!("{}", fib(10));
}

image-20201228205112046