Closures
Closures are anonymous functions that capture variables: |x| x + 1.
Example
fn main() {
let k = 10;
let add = |x| x + k;
let sq = |x: i32| -> i32 { x * x };
println!("{} {}", add(5), sq(4));
}Result
15 16
Exercise
Add one.
let f = |x| x 1;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.