As the error message says:
cannot assign to captured outer variable in an Fn closure
Instead, you want a FnMut closure:
// In modern Rust, prefer returning `impl FnMut() -> i32`
fn counter() -> Box<dyn FnMut() -> i32> {
let mut c = 0;
Box::new(move || {
c += 1;
c
})
}
fn main() {
let mut a = counter();
let mut b = counter();
let result = [a(), a(), a(), b(), b(), a()];
println!("{:?}", result);
assert_eq!([1, 2, 3, 1, 2, 4], result);
}
As the FnMut docs say:
The version of the call operator that takes a mutable receiver.
This allows the closure to mutate the contained state.
Incidentally, the explicit type for c is not needed.
What confuses me, is that pub trait Fn<Args>: FnMut<Args>. Doesn't it mean that Fn (what I used) should support behaviour of FnMut?
Perhaps When does a closure implement Fn, FnMut and FnOnce? can help provide some background information. This is an aspect I get intuitively, but haven't figured out how best to communicate. This section from Finding Closure in Rust also seems relevant:
At a high-level self gives implementers (i.e. the types users define to implement the trait) the most flexibility, with &mut self next and &self the least flexible. Conversely, &self gives consumers of the trait (i.e. functions with generics bounded by the trait) the most flexibility, and self the least.
In short, that trait definition says that any Fn closure can be used where a FnMut closure is expected. This makes some sense, as we can ignore the mutability available to the FnMut. You cannot go the other way - you cannot make an immutable reference into a mutable one.