updated: 5th of March 2022
published: 25th of February 2022
A closure in Rust is an anonymous function. An anonymous function is a function without a name. A closure is defined with parameters between two pipes and expressions between curly braces || {} .
// Closure with a single parameter and single expression.
// Curly braces are optional with a single expression.
|param| expression;
// Closure with multiple parameters and multiple expressions.
// Curly braces are required for more than 1 expression.
|param_1, param_2| { expression_1 ; expression_2 };
// Assign a closure to the variable `add`.
let add = |a, b| a + b;
// Call a closure
add(1, 2); // => 3
Unlike functions, closures CAN capture variables from their enclosing scope. By default, closures borrow a reference to variables from their enclosing scope.
let one = "one".to_string();
// Closure that `borrows` a reference to variable from enclosing scope.
let f = || println!("{}", one);
// Borrow a reference to `one`.
f(); // => one
Implement move semantics on a closure, to move the variable ownership to the closure.
let one = "one".to_string();
// Closure that `moves` the captured variables from the enclosing scope into its scope.
let f = move || println!("{}", one);
// Move the value of `one` into the closure.
f(); // => one
// `one` is no longer valid.
println!("{}", one) // => error: value borrowed after move
https://www.manning.com/books/rust-in-action
https://www.udemy.com/course/ultimate-rust-crash-course/
https://doc.rust-lang.org/rust-by-example/fn/closures.html
https://doc.rust-lang.org/book/ch13-01-closures.html
https://medium.com/coding-rust/best-explanation-of-closure-in-rust-2b20210eba53