How to pass closure to function

Pass a closure to a function by using a generic parameter with a trait bound like Fn, FnMut, or FnOnce.

Pass a closure to a function by defining the parameter with a closure type (like Fn, FnMut, or FnOnce) or using a generic with a trait bound, then call it inside the function body.

fn apply<F>(x: i32, operation: F) -> i32
where
    F: Fn(i32) -> i32,
{
    operation(x)
}

fn main() {
    let double = |x| x * 2;
    let result = apply(5, double);
    println!("{result}");
}