從函式中返回 lambdas
從函式返回 lambdas(或閉包)可能很棘手,因為它們實現了特徵,因此很少知道它們的確切大小。
// Box in the return type moves the function from the stack to the heap
fn curried_adder(a: i32) -> Box<Fn(i32) -> i32> {
// 'move' applies move semantics to a, so it can outlive this function call
Box::new(move |b| a + b)
}
println!("3 + 4 = {}", curried_adder(3)(4));
這顯示:3 + 4 = 7