>>106521416
>how does rust manage to take the code on the right and turn it into basically whats on the left?
It's literally just monomorphization and function inlining of the most basic kind.
sum is just fold with Add::add as the reducer, fold is just for(while let):
let mut accum = init;
while let Some(x) = self.next() {
accum = f(accum, x);
}
accum
Looks similiar? It's taken from stdlib. f here is the Add::add, self.next is just the map+filter+filter+iter stack that just transform or skip values from iter. When you untangle it all you get very similar code except there is bunch of lambdas out there. But these lambdas are just free functions and Rust compiler can inline them all and in the end you get pretty much same code with only artificial syntactic differences.