Rust Closures and lambda expressions Simple closures


Example

Unlike regular functions, lambda expressions can capture their environments. Such lambdas are called closures.


// variable definition outside the lambda expression...
let lucky_number: usize = 663;

// but the our function can access it anyway, thanks to the closures
let print_lucky_number = || println!("{}", lucky_number);

// finally call the closure
print_lucky_number();

This will print:

663