Help determining how to use functional programming and classical constructs?

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);

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?

I'm not the best person to answer this but here is my experience:

  1. As a noob to Rust I try to discover the "idiomatic way". Which from reading here a while indicates I should get into iterators and all that functional programming style. That's all new and alien to me but I try to get used to it.

  2. Sometimes, after finding the iterator/functional way was horribly verbose, obscure and worst of all slow I ask for advice about it here.

  3. After much discussion it is concluded that good old style loop is the best way to get the job done.

My conclusion is that there is no hard and fast rule about this. I'm going to use iterators and such where they look nice, don't obscure what is going on and don't impact performance. Otherwise I'm going to make my code look as much like C as possible :slight_smile:

Yeah, that's sometimes what I do too. Was just wondering though since sometimes the more"functional" way seems a lot nicer.


I'm sure it depends a lot on where you come from. A lifelong C user might find it all alien gobbledygook, as I did, using all kind conceptual baggage like all the .zip, .fold, lambdas etc where a simple "for" loop and some "if"s would do.

Someone coming from the rarefied heights of Haskell will see it as natural.

I have stopped worrying about it. I write my code so that I will understand it next month when I read it again. If Rust is happy and clippy is happy then I'm happy.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.