-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
As C++ programmers, we must often deal with performance-sensitive code. It is natural for us to wonder about the runtime performance of these chains of potentially complicated iterator expressions. But as a result of lazy evaluation, generic function evaluation, and extensive inlining, performance in optimized builds ends up being quite good. In some tests, it has even been shown to outperform a hand-written loop. In this section, we'll examine how the Rust compiler can perform such a feat.
It might be hard to believe that iterator performance can beat a hand-written loop without seeing it in action, so let's go through a simple example. We'll dive deeply into an iterator expression, unrolling and inlining it by hand to get a sense of how the compiler will see it.
Our expression will be the relatively simple v.into_iter().filter(|x| x % 2 == 0).map(|x| x * 2).sum(), where v is a vector of u32...