expression
Rust Create a small closure
A closure is callable code created as an expression. It can use local values from its surrounding scope. This example reads a rate rather than receiving it as another argument. Expected output (not executed): 12 Why: The closure uses rate from its enclosing scope. Common mistake: Do not assume every closure owns its captures; capture mode depends on use and the presence of move.
Formal Syntax & Structure
rust syntaxrust
|parameter| expressionWorking Example
Example Snippetrust
fn main() { let rate = 3; let charge = |units: i32| units * rate; println!("{}", charge(4));}