mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-14 17:42:41 +00:00
26 lines
430 B
Rust
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();
|
|
}
|
|
|