闭包

闭包是可以捕获其封闭环境的函数。例如,捕获 x 变量的闭包:

|val| val + x

闭包的语法和功能使其非常方便地用于即时使用。调用闭包就像调用函数一样。然而,输入和返回类型可以被推断出来,并且必须指定输入变量的名称。

闭包的其他特性包括:

  • 在输入变量周围使用 || 而不是 ()
  • 单行表达式的可选主体定界符 ({})(否则是强制性的)。
  • 能够捕获外部环境的变量。
fn main() {
    let outer_var = 42;
    
    // A regular function can't refer to variables in the enclosing environment
    //fn function(i: i32) -> i32 { i + outer_var }
    // TODO: uncomment the line above and see the compiler error. The compiler
    // suggests that we define a closure instead.

    // Closures are anonymous, here we are binding them to references.
    // Annotation is identical to function annotation but is optional
    // as are the `{}` wrapping the body. These nameless functions
    // are assigned to appropriately named variables.
    let closure_annotated = |i: i32| -> i32 { i + outer_var };
    let closure_inferred  = |i     |          i + outer_var  ;

    // Call the closures.
    println!("closure_annotated: {}", closure_annotated(1));
    println!("closure_inferred: {}", closure_inferred(1));
    // Once closure's type has been inferred, it cannot be inferred again with another type.
    //println!("cannot reuse closure_inferred with another type: {}", closure_inferred(42i64));
    // TODO: uncomment the line above and see the compiler error.

    // A closure taking no arguments which returns an `i32`.
    // The return type is inferred.
    let one = || 1;
    println!("closure returning one: {}", one());

}