comprehensive-rust
comprehensive-rust copied to clipboard
Factual error: course says 'move' closures can only be FnOnce, not true
Hey, amazingly written course!
In section 25.8 (Important Traits / Closures ...), it is stated that:
move closures only implement FnOnce.
That is not true. What move closures do is that they move their captured values into the closure itself instead of just borrowing them (immutably or mutably). It gives the closure an exclusive access to the captures, but the closed over values only get deallocated when the closure itself gets deallocated. Thus it's perfectly fine to have a move closure be FnMut or even Fn.
Here's an example of a move closure being FnMut (playground link)
fn call_3_times(mut f: impl FnMut()) {
f();
f();
f();
}
fn main() {
let mut v = vec![];
let f = move || {
v.push(1);
println!("{:?}", v);
};
call_3_times(f);
}
Hi @faiface!
Thanks for the bug report, you're quite right! Would be up for writing a fix for the speaker notes?