Closures are similar to functions except they can be stored in a variable and can capture the surrounding variables.
Example:
fn main() {
// Normal Closure
let add_one = |x| {
x + 1
};
// Closures are of a single concrete type
let y = add_one(1); // The type x is deduced to be an i32
//let y = add_one(1.0); // Does not compile because x should be an i32, not f32
// Type information optional
let verbose_add_one = |x: i32| -> i32 {
x + 1
};
// {} optional if single expression
let one_linner = |x| x + 1;
let z = one_linner(2);
}
Closures can be returned from functions via a pointer.
fn returns_closure() -> Box<dyn Fn(u32) -> u32> {
Box::new(|x| x * x)
}
Closures can implement some or all of three traits:
Fn
: Borrows the values from the environment immutablyFnMut
: Mutably borrow valuesFnOnce
: Consumes the variables it captures from its environment. This closure can be called only once because it consumes the variables.struct Cacher<T>
where
T: Fn(i32) -> i32,
{
func: T,
value: Option<i32>,
}
impl<T> Cacher<T>
where
T: Fn(i32) -> i32,
{
fn new(func: T) -> Cacher<T> {
Cacher{
func,
value: None,
}
}
fn value(&mut self, arg: i32) -> i32 {
match self.value {
Some(v) => v,
None => {
let v = (self.func)(arg);
self.value = Some(v);
v
}
}
}
}
fn main() {
let add = 5;
let mut saved_value = Cacher::new(|x| x + add);
println!("{}", saved_value.value(2)); // 7
println!("{}", saved_value.value(738)); // 7, because cached
}