What is the difference between Fn FnMut FnOnce

Fn allows multiple immutable calls, FnMut allows multiple mutable calls, and FnOnce allows a single call that consumes captured values.

The difference lies in how many times a closure can be called and whether it mutates captured data. Fn can be called multiple times and borrows captures immutably; FnMut can be called multiple times and mutates captures; FnOnce can be called exactly once and takes ownership of captures. Use Fn for read-only logic, FnMut for stateful logic, and FnOnce for one-time actions like moving resources.

let x = 5;
let read_only = || println!("{}", x); // Fn
let mut state = 0;
let mutator = || state += 1; // FnMut
let owned = String::from("hello");
let consumer = || println!("{}", owned); // FnOnce