Description
This code:
fn counter() -> Box<Fn() -> usize> {
let mut x = 0;
Box::new(move || {
x += 1;
x
})
}
results in the following error messages in stable:
<anon>:4:9: 4:15 error: cannot assign to captured outer variable in an `Fn` closure
<anon>:4 x += 1;
^~~~~~
<anon>:3:14: 6:6 help: consider changing this closure to take self by mutable reference
<anon>:3 Box::new(move || {
<anon>:4 x += 1;
<anon>:5 x
<anon>:6 })
The problem here is that we're using Fn()
instead of FnMut()
, but the phrase "consider changing this closure to take self by mutable reference" is not particularly useful in telling us that that specifically is the problem. That is, the fact that we cannot assign to the captured outer variable is because we are trying to return Box<Fn() -> usize>
instead of Box<FnMut() -> usize>
, but that's not immediately clear from the original error message.
I'm not entirely sure how this would be rectified, but at the very least mentioning FnMut
somewhere would be useful. @eddyb mentioned that this issue was "because a bound/trait object of Fn()
was used but only FnMut()
allows mutable borrows", so I think some variation on that would be reasonably sufficient to clarify the error.