Can this recursive function taking an iterator be written in a more idiomatic way?

I wrote a tiny piece of code that works but I have the feeling there is a more clever way to do it. the purpose of the code is to recursively apply a fused multiply add function to the items in an iterator. Currently this code works as it should, but can it be written more idiomatic?

The second approach in the following playground should be equivalent, though it does require the iterator to support double ended access, so it can precisely replicate the right order of fused_mul_add calls. In case you don't care about the order, you could also skip the rev() step, and then it would work with every Iterator :slight_smile:

Rust Playground

1 Like

Exactly what I was looking for, thanks a lot!

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.