How to avoid ownership issues with closures

Avoid ownership issues in Rust closures by capturing variables by reference instead of moving them.

Avoid ownership issues by capturing values by reference instead of moving them into the closure.

let data = vec![1, 2, 3];
let closure = || {
    // Access data by reference, not ownership
    println!("{:?}", data);
};
closure();
println!("{:?}", data); // data is still usable here

If the closure must own the data, use the move keyword explicitly to transfer ownership, ensuring the original variable cannot be used afterward.