通过例子学习Rust

35 闭包

Closures are special functions that can capture the variables available in the surrounding scope. Closures consist of three parts:

  • A list of arguments enclosed by pipes |, these arguments can optionally be type annotated, but usually the compiler will infer their types
  • Optionally the return type using an arrow ->, again this usually gets inferred
  • A block, the last expression is the return value
fn main() { let captured_value = 7u; let closure = |argument| { println!("I captured this: {}", captured_value); println!("Argument passed was: {}", argument); true }; println!("Closure returned: {}", closure("a string")); }