Rust by Example

31 Closures

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 = 7u32; let closure = |&:argument| { println!("I captured this: {}", captured_value); println!("Argument passed was: {}", argument); true }; println!("Closure returned: {}", closure("a string")); }