I got stuck on another Rust closure puzzle today. What I'm trying to do boils down to this, which won't compile:
fn main() {
let buffers: [[u8; 2]; 2] = [[1, 2], [3, 4]];
let mut mysum: u8 = 0;
let my_chksum = |c| {
mysum += c;
};
for buf in buffers {
generic_sum(&buf, my_chksum);
}
println!("My checksum is {}", mysum); // Expecting 10
}
fn generic_sum<F>(buf: &[u8], mut summer: F) where
F: FnMut(u8) {
for c in buf {
summer(*c);
}
}
This gives me the error
closure cannot be moved more than once as it is not
Copy
due to moving the variablemysum
out of its environment
I shouldn't be actually moving it, since it's FnMut not FnOnce, right?
I can move the closure definition inside the for buf in buffers
loop, which seems to work. Is that efficient? Is there a better way to solve this?