Blog: Map of a lifetime


Wherein we iterate over combined index pairs.


Let’s make the iterator fast!


I’d like to first improve the error message. A suggestion to move into the closure would be appreciated.


I think the error makes sense given Rust always prefers to borrow captured values in closures. I do agree the compiler could probably generate a hint to look at moving the value if it’s Copy.


Why is the iterator so slow?


The simple example in this blog is a good dose of reality for the recent zero cost abstractions thread :slight_smile:


Well I don’t know how much it is lagging behind for larger sizes:

running 2 tests
test bench_flat_map     ... bench:       3,474 ns/iter (+/- 183)
test bench_nested_loops ... bench:       3,115 ns/iter (+/- 113)


test bench_flat_map              ... bench:     309,322 ns/iter (+/- 16,184)
test bench_nested_loops      ... bench:     304,257 ns/iter (+/- 6,455)

That is 111% and 102% of the execution time of the nested loop. My guess is that as it grows larger it will get closer to 100%. And for 10 I get it only x2 slower. I do agree however that it would be nice to have such sort of optmizations out of the box.