Knowledge Garden

Search

Search IconIcon to open search

Closures in Rust

Last updated Dec 1, 2022 Edit Source

# Closures

inspired by ruby and smalltalk

closure is an anonymous function that can borrow or capture data from the scope it is nested in.

a parameter list between two pipes without type notions and the function between two curly braces.

1
| x,y | { x + y } 

A closure will borrow references to values in the enclosing scope. This creates an anonymous function you can call later.

1
2
let add = |x,y|{x+y};
add(1,2);

you don’t need parameters.

1
||{x+2}

closures can also take references to values in the current scope.

1
2
3
4
5
6
let s = "🍓".to_string();
ket f = || {
	println!("{}",s);
};

f(); // prints 🍓

the compiler won’t let us move this to another thread because this might live longer than another thread. But there is move support for closures to remedy this.

1
2
3
4
5
6
let s = "🍓".to_string();
ket f = move || {
	println!("{}",s);
};

f(); // prints 🍓

this forces the closure to move any variables into itself and take ownership of them.

functional programming, closures will be your friend

1
2
3
4
5
let mut v = vec![2,4,6];
v.iter()
	.map(|x|x*3)
	.filter(|x|*x>10)
	.fold(0,|acc,x| acc+x);