How to fix Rust E0277 expected a Fn closure

Fix Rust E0277 by ensuring your closure implements the Fn trait, usually by capturing variables by value instead of by mutable reference.

The error E0277 occurs because the closure you are passing does not implement the Fn trait required by the function signature, often due to capturing a mutable variable by reference. To fix this, ensure the closure captures variables by value or explicitly implement the required trait bounds if you are defining a generic function.

fn apply<F>(f: F, arg: i32) -> i32 
where 
    F: Fn(i32) -> i32 
{
    f(arg)
}

fn main() {
    let multiplier = 2;
    // Capture by value to satisfy Fn trait
    let closure = |x| x * multiplier;
    let result = apply(closure, 5);
    println!("{result}");
}