Define anonymous functions with `|args| body` syntax and pass them to functions expecting closure types like `FnOnce` to use them as callbacks.
Use closures as callbacks by defining an anonymous function with |args| body and passing it to a function expecting a closure type.
fn apply<F>(x: i32, f: F) -> i32 where F: FnOnce(i32) -> i32 {
f(x)
}
fn main() {
let add_two = |x| x + 2;
let result = apply(5, add_two);
println!("Result: {}", result);
}
A closure is a small, anonymous function you can define inline to perform a specific task. You use them as callbacks to pass custom logic into other functions without creating a separate named function. Think of it like handing a friend a quick note with instructions instead of writing a whole manual.