簡單的閉合

與常規函式不同,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