-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosures.rs
More file actions
39 lines (31 loc) · 1.2 KB
/
closures.rs
File metadata and controls
39 lines (31 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! closures.rs
//!
//! Introduction to Rust closures — anonymous functions that can capture
//! variables from their environment. Shows different capture modes and
//! common use cases with iterators.
fn main() {
// 1. Basic closure
let add: fn(i32, i32) -> i32 = |a: i32, b: i32| a + b;
println!("2 + 3 = {}", add(2, 3));
// 2. Closure that captures a variable from the environment
let factor: i32 = 10;
let multiply_by_factor = |x: i32| x * factor; // captures factor by reference
println!("7 * 10 = {}", multiply_by_factor(7));
// 3. Mutable closure (captures by mutable reference)
let mut counter: i32 = 0;
let mut increment = || {
counter += 1;
counter
};
println!("Counter: {}", increment());
println!("Counter: {}", increment());
println!("Counter: {}", increment());
// 4. Common use case: passing closure to iterator methods
let numbers: Vec<i32> = vec![1, 2, 3, 4, 5, 6];
let squares: Vec<i32> = numbers
.iter()
.map(|&x| x * x) // closure passed to map
.filter(|&x| x > 10) // another closure
.collect();
println!("\nSquares greater than 10: {:?}", squares);
}