rust-ape-example/src/bin/fn_closures_anonymity.rs
2022-09-07 10:49:49 +05:30

26 lines
430 B
Rust

// ./src/fn/closures/anonymity.md
// `F` must implement `Fn` for a closure which takes no
// inputs and returns nothing - exactly what is required
// for `print`.
fn apply<F>(f: F) where
F: Fn() {
f();
}
fn part0() {
let x = 7;
// Capture `x` into an anonymous type and implement
// `Fn` for it. Store it in `print`.
let print = || println!("{}", x);
apply(print);
}
pub fn main() {
part0();
}