简单的闭合

与常规函数不同,lambda 表达式可以捕获它们的环境。这种 lambdas 被称为闭包。

// 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();

这将打印:

663