So, I've struggled with this for many years now trying to figure this out in a way I can understand. I love iterators and the functions they provide (map, chunks, step_by, skip, ...). However, I come from a procedural/OOP background, which makes this transition to the "functional" way difficult for me in some areas (which is why Rust is so nice -- it lets me do either if I want to). What I'm struggling with is this: what is a nice way of figuring out when I should use a classical loop (for, while, loop) vs. a functional method (map, for_each, ...)? Take for example this loop for calculating CRCs:
for i in buf.iter() {
crc32 = CRC32_TAB[(crc32 ^ i) & 0xFF] ^ (crc32 >> 8);
}
Or this set of loops, nested within each other:
// Compute CRC32 of partition array
for address in self.header.part_entry_lba..self.header.first_usable_lba {
let lba = (self.read_func)(address, 1);
for partition_bytes in lba.chunks(self.header.part_size as usize) {
crc_bytes.resize(crc_bytes.len() + partition_bytes.len(), 0);
crc_bytes.extend_from_slice(partition_bytes)?;
}
}
In both of these loops, I haven't really used the full power of iterators. So, what are a good set of rules to remember/master about when to use advanced iterator methods (map, for_each, scan, ...) vs. classical iterator loops like the ones above? And is there some way of figuring out when to use either method of iteration?