let mut count = 0;
// A closure to increment `count` could take either `&mut count` or `count`
// but `&mut count` is less restrictive so it takes that. Immediately
// borrows `count`.
//
// A `mut` is required on `inc` because a `&mut` is stored inside. Thus,
// calling the closure mutates the closure which requires a `mut`.
let mut inc = || {
count += 1;
println!("`count`: {}", count);
};
The documentation states that the &mut
variable that stores the count in the closure, but shouldn't the count here be itself? Or is the c-operation bbf of +=
essentially an operation via &mut count
? Or is it &mut count
in the closure that is dereferenced when the function is executed, *&mut count+=1
?