Return a closure from a Rust function by defining the return type as impl Fn and using move to capture environment variables.
Define a function that returns a closure type impl Fn and use the move keyword if the closure needs to own captured variables.
fn make_adder(x: i32) -> impl Fn(i32) -> i32 {
move |y| x + y
}
fn main() {
let adder = make_adder(10);
println!("{}", adder(5)); // Prints 15
}
Returning a closure from a function creates a custom factory that remembers specific values you provide. It's like handing someone a calculator pre-loaded with a number so they only need to supply the second number to get a result. You use this when you need to pass around small, anonymous logic that depends on surrounding data.