---
title: "Zero-Cost Abstractions: How Rust Optimizes Iterator Chains"
description: "Low-level optimization in Rust, focusing on iterator chains and zero-cost abstractions"
url: "https://mayorana.ch/en/blog/zero-cost-abstractions-rust"
language: "en"
date: "2025-09-12"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "optimization"
  - "advanced"
fr_version: "https://mayorana.ch/fr/blog/zero-cost-abstractions-rust.md"
---

# Zero-Cost Abstractions: How Rust Optimizes Iterator Chains

Rust’s **zero-cost abstractions** allow high-level constructs, like iterator chains, to compile into machine code as efficient as hand-written loops, with no runtime overhead. This is critical for performance-sensitive systems. Below, I explain how the Rust compiler transforms an iterator chain (e.g., using `map`, `filter`, and `collect`) into an efficient loop, focusing on inlining and loop fusion, and how to verify the optimization in practice.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo2-fig" viewBox="0 0 800 300" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Iterator chain of filter, map, and collect being inlined and fused by the compiler into a single tight loop">
<style>
.lo2-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo2-fig,[data-theme="dark"] .lo2-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo2-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo2-fig .mid{fill:var(--box);stroke:var(--mut);stroke-width:1.5}
.lo2-fig .fin{fill:var(--box);stroke:var(--ac);stroke-width:2.5}
.lo2-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig .ac{fill:var(--ac);font:700 13px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig line,.lo2-fig path.ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="lo2-arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="var(--ln)"/>
</marker>
<marker id="lo2-arrow-ac" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="var(--ac)"/>
</marker>
</defs>
<!-- top row: three iterator adapters -->
<rect x="30" y="30" width="200" height="55" rx="6" class="box"/>
<text x="130" y="55" class="tx">.filter(x % 2 == 0)</text>
<text x="130" y="72" class="mut">skip odd numbers</text>
<rect x="300" y="30" width="200" height="55" rx="6" class="box"/>
<text x="400" y="55" class="tx">.map(x * 2)</text>
<text x="400" y="72" class="mut">double each value</text>
<rect x="570" y="30" width="200" height="55" rx="6" class="box"/>
<text x="670" y="55" class="tx">.collect()</text>
<text x="670" y="72" class="mut">size_hint pre-allocates</text>
<!-- connect the three adapters -->
<line x1="230" y1="57" x2="298" y2="57" marker-end="url(#lo2-arrow)"/>
<line x1="500" y1="57" x2="568" y2="57" marker-end="url(#lo2-arrow)"/>
<!-- Y-merge into compiler stage -->
<path class="ln" d="M130,85 L130,110 L400,110"/>
<path class="ln" d="M670,85 L670,110 L400,110"/>
<line x1="400" y1="110" x2="400" y2="138" marker-end="url(#lo2-arrow)"/>
<rect x="230" y="140" width="340" height="55" rx="6" class="mid"/>
<text x="400" y="163" class="tx">Inlining + Loop Fusion</text>
<text x="400" y="180" class="mut">LLVM merges next() calls into one pass</text>
<!-- final tight loop -->
<line x1="400" y1="195" x2="400" y2="223" marker-end="url(#lo2-arrow-ac)"/>
<rect x="210" y="225" width="380" height="55" rx="6" class="fin"/>
<text x="400" y="248" class="ac">Single tight loop</text>
<text x="400" y="265" class="mut">cmp / test / lea / mov — no call overhead</text>
</svg>
</div>

## How the Compiler Optimizes Iterator Chains

Consider this example:

```rust
let numbers: Vec<i32> = (0..100)
    .filter(|&x| x % 2 == 0)  // Keep even numbers
    .map(|x| x * 2)           // Double them
    .collect();               // Gather into a Vec
```

This high-level code appears to involve multiple passes over the data, but Rust’s compiler (via LLVM) transforms it into a single, efficient loop comparable to manual code. Here’s how:

- **Inlining**: Each iterator adapter (`filter`, `map`) is a struct implementing the `Iterator` trait with a `next()` method. The compiler inlines these `next()` calls, eliminating function call overhead. For `filter`, `next()` skips non-matching elements; for `map`, it applies the transformation. Inlining exposes the logic to further optimization.
- **Loop Fusion**: After inlining, the compiler sees a sequence of operations on the same data stream. It fuses these into a single loop, avoiding intermediate allocations or multiple traversals. The above chain becomes roughly equivalent to:
  ```rust
  let mut numbers = Vec::with_capacity(50); // Pre-allocates, thanks to size hints
  for x in 0..100 {
      if x % 2 == 0 {
          numbers.push(x * 2);
      }
  }
  ```
  LLVM’s loop optimization pass combines the condition and transformation into one iteration.
- **Iterator Size Hints**: Rust iterators provide `size_hint()` to estimate output length. Here, `collect()` uses this to [pre-allocate](https://mayorana.ch/en/blog/vec-new-vs-with-capacity) the `Vec`, avoiding [reallocations](https://mayorana.ch/en/blog/vec-push-vs-with-capacity-performance)—a key efficiency win.
- **Dead Code Elimination and Simplification**: Rust’s ownership and type system ensure no runtime reference counting or unnecessary bounds checks persist. LLVM further simplifies arithmetic or removes redundant branches (e.g., constant folding in complex closures).

The result is a tight loop with no abstraction penalty, matching the performance of C-style code, as Rust’s type safety and iterator design give the compiler full visibility into the data flow.

## Role of Inlining and Loop Fusion

- **Inlining**: The linchpin of optimization, inlining eliminates the overhead of separate function calls for each iterator adapter, exposing the logic for further optimization.
- **Loop Fusion**: Merges multiple iterator operations into a single loop, leveraging monomorphization (for generic iterators) and LLVM’s aggressive optimizations. This ensures the abstraction incurs no runtime cost—you pay only for the operations you use.

## Verifying the Optimization

To confirm this efficiency in practice, use these techniques:

- **Assembly Inspection**: Run `cargo rustc --release -- --emit asm` or use `godbolt.org` with `-O3` to view the generated assembly. Look for a single loop (e.g., `cmp`, `jne`, `add` instructions on x86_64) with no extra jumps or allocations beyond `Vec` growth.
- **Benchmarking**: Use `criterion` to measure runtime against a hand-written loop:
  ```rust
  use criterion::{black_box, Criterion};
  fn bench(c: &mut Criterion) {
      c.bench_function("iterator_chain", |b| b.iter(|| {
          black_box((0..100).filter(|&x| x % 2 == 0).map(|x| x * 2).collect::<Vec<i32>>())
      }));
  }
  ```
  Compare this to a manual loop’s performance—times should be nearly identical in release mode.
- **Profiling**: With `perf` on Linux (`perf stat -e instructions,cycles`), check instruction count and cycles. A fused loop should show minimal overhead versus the baseline.
- **Debug vs. Release**: Compile with `--debug` and `--release` to see the difference. Debug mode might show separate iterator steps, while release mode fuses them, proving the optimization.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo2-fig2" viewBox="0 0 800 375" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="In a debug build each iterator adapter costs a nested next() stack frame per element, while the release build collapses them into one fused loop body with no call instructions">
<style>
.lo2-fig2{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo2-fig2,[data-theme="dark"] .lo2-fig2{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo2-fig2 .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo2-fig2 .fin{fill:var(--box);stroke:var(--ac);stroke-width:2.5}
.lo2-fig2 .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig2 .ti{fill:var(--tx);font:700 13px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig2 .ac{fill:var(--ac);font:700 13px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig2 .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo2-fig2 line,.lo2-fig2 path.ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="lo2b-arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="var(--ln)"/>
</marker>
</defs>
<!-- left: debug nesting -->
<text x="190" y="22" class="ti">Debug build — 4 stack frames per element</text>
<rect x="40" y="38" width="300" height="40" rx="5" class="box"/>
<text x="190" y="63" class="tx">collect() calls next()</text>
<line x1="235" y1="78" x2="235" y2="96" marker-end="url(#lo2b-arrow)"/>
<rect x="70" y="98" width="270" height="40" rx="5" class="box"/>
<text x="205" y="123" class="tx">Map::next() — x * 2</text>
<line x1="235" y1="138" x2="235" y2="156" marker-end="url(#lo2b-arrow)"/>
<rect x="100" y="158" width="240" height="40" rx="5" class="box"/>
<text x="220" y="183" class="tx">Filter::next() — x % 2</text>
<line x1="235" y1="198" x2="235" y2="216" marker-end="url(#lo2b-arrow)"/>
<rect x="130" y="218" width="210" height="40" rx="5" class="box"/>
<text x="235" y="243" class="tx">Range::next()</text>
<!-- right: release fused -->
<text x="600" y="22" class="ti">Release build — inlining + fusion</text>
<rect x="430" y="38" width="340" height="220" rx="6" class="fin"/>
<text x="600" y="110" class="ac">One fused loop body</text>
<text x="600" y="136" class="tx">no Map / Filter structs remain</text>
<text x="600" y="160" class="mut">1 stack frame, 0 call instructions</text>
<!-- merge into verification -->
<path class="ln" d="M235,258 L235,286 L400,286"/>
<path class="ln" d="M600,258 L600,286 L400,286"/>
<line x1="400" y1="286" x2="400" y2="300" marker-end="url(#lo2b-arrow)"/>
<rect x="210" y="302" width="380" height="58" rx="6" class="box"/>
<text x="400" y="327" class="tx">Verify: --emit asm on both builds</text>
<text x="400" y="347" class="mut">release should contain no call inside the loop</text>
</svg>
</div>

## Example Outcome

In the assembly for the example, expect a loop like:

```text
loop:
    cmp eax, 100       ; Check range bound
    jge done
    test eax, 1        ; Check evenness
    jnz skip
    lea ebx, [eax*2]   ; Double the value
    mov [rdi], ebx     ; Store in Vec
    add rdi, 4         ; Advance pointer
skip:
    inc eax            ; Next iteration
    jmp loop
```

This shows no extra iterator structs or calls—just raw arithmetic and memory ops, matching a manual implementation.

## So is it actually zero-cost?
Rust’s compiler transforms iterator chains into efficient loops via inlining and loop fusion, fulfilling the zero-cost abstraction promise. As a developer, I’d verify this with assembly analysis and benchmarks using tools like `cargo asm`, `godbolt.org`, and `criterion`, ensuring the abstraction doesn’t compromise performance in a production system. This allows writing clean, maintainable code without sacrificing speed.
