Skip to main content

Iterator chains that stay fast

Iterator adapters are lazy: `map` and `filter` build a description of the work and nothing happens until a consuming call like `collect`, `sum` or `for` asks for values.

Because the whole chain is inlined and monomorphised, an idiomatic chain usually compiles to the same machine code as a hand-written loop. Readability here is free.

`collect` is the one that surprises people: it can produce a `Vec`, a `String`, a `HashMap` or a `Result`, so the target type has to be inferable or written out.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust