---
title: "Flatten a Vec<Vec<T>> into a Vec<T> using iterators"
description: "Flattening Vec<Vec<T>> using iterators compared to manual concatenation, analyzing performance implications"
url: "https://mayorana.ch/en/blog/flatten-vec-iterators-performance"
language: "en"
date: "2025-07-29"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "vec"
fr_version: "https://mayorana.ch/fr/blog/flatten-vec-iterators-performance.md"
---

# Flatten a Vec<Vec<T>> into a Vec<T> using iterators

## Flattening with Iterators

The most idiomatic way is to use `.flatten()` or `.flat_map()`:

<div class="svg-container" style="margin:2rem 0;">
<svg class="ci9-fig" viewBox="0 0 800 220" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Three inner vectors merge through flatten into one flat Vec of six elements">
<!-- style -->
<style>
.ci9-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .ci9-fig,[data-theme="dark"] .ci9-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.ci9-fig .bg{fill:var(--bg)}
.ci9-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.ci9-fig .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.ci9-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif}
.ci9-fig .title{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif}
.ci9-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif}
.ci9-fig .ac{fill:var(--ac)}
.ci9-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- defs -->
<defs>
<marker id="ci9-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--ln)"/></marker>
<marker id="ci9-arrowac" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--ac)"/></marker>
</defs>
<!-- bg -->
<rect class="bg" x="0" y="0" width="800" height="220" rx="8"/>
<!-- title -->
<text x="400" y="24" text-anchor="middle" class="title">Vec&lt;Vec&lt;T&gt;&gt; → flatten() → Vec&lt;T&gt;</text>
<!-- three inner vecs -->
<rect class="box" x="60" y="46" width="150" height="34" rx="6"/>
<text x="135" y="67" text-anchor="middle" class="tx">[1, 2]</text>
<rect class="box" x="325" y="46" width="150" height="34" rx="6"/>
<text x="400" y="67" text-anchor="middle" class="tx">[3]</text>
<rect class="box" x="590" y="46" width="150" height="34" rx="6"/>
<text x="665" y="67" text-anchor="middle" class="tx">[4, 5, 6]</text>
<!-- merge lines to Y point -->
<path class="ln" d="M135 80V100"/>
<path class="ln" d="M400 80V100"/>
<path class="ln" d="M665 80V100"/>
<path class="ln" d="M135 100H665"/>
<path class="ln" d="M400 100V120" marker-end="url(#ci9-arrow)"/>
<!-- flatten box -->
<rect class="acbox" x="320" y="120" width="160" height="36" rx="6"/>
<text x="400" y="143" text-anchor="middle" class="tx ac">.flatten().collect()</text>
<!-- arrow to result -->
<path class="ln" d="M400 156V178" marker-end="url(#ci9-arrow)"/>
<rect class="box" x="270" y="178" width="260" height="30" rx="6"/>
<text x="400" y="198" text-anchor="middle" class="tx">[1, 2, 3, 4, 5, 6]</text>
</svg>
</div>

```rust
let nested = vec![vec![1, 2], vec![3], vec![4, 5, 6]];

// Method 1: flatten() (for Vec<Iterables>)
let flat: Vec<_> = nested.iter().flatten().copied().collect();

// Method 2: flat_map() (for custom transformations)
let flat: Vec<_> = nested.into_iter().flat_map(|v| v).collect();
```

**Output**: `[1, 2, 3, 4, 5, 6]`

## Manual Concatenation

For comparison, here's how you might do it manually:

```rust
let mut flat = Vec::new();
for subvec in nested {
    flat.extend(subvec);  // or append() if subvec is no longer needed
}
```

## Performance Comparison

| Method | Time Complexity | Space Complexity | Allocations | Optimizations |
|--------|-----------------|------------------|-------------|---------------|
| Iterator (flatten) | O(n) | O(1) iterator | 1 (result) | May fuse iterators |
| Manual (extend) | O(n) | O(1) temp space | 1 (result) | Pre-allocation possible |

## Key Insights

### Pre-allocation Advantage (Manual)

You can [pre-allocate](https://mayorana.ch/en/blog/vec-new-vs-with-capacity) the target Vec if total size is known:

```rust
let total_len: usize = nested.iter().map(|v| v.len()).sum();
let mut flat = Vec::with_capacity(total_len);  // Critical for large datasets
flat.extend(nested.into_iter().flatten());
```

### Iterator Laziness

- `.flatten()` is lazy, but `.collect()` still needs to allocate the result.
- Chained iterators (e.g., `.filter().flatten()`) may optimize better than manual loops.

Laziness here means `flatten()` keeps just two things alive — the outer iterator and the inner iterator it is currently draining — and does all its work inside a single `next()` call:

<div class="svg-container" style="margin:2rem 0;">
<svg class="ci9b-fig" viewBox="0 0 800 320" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Decision flow for one next call on flatten: yield from the current inner iterator, or when it is exhausted pull the next inner collection from the outer iterator, or finish when the outer is done">
<!-- style -->
<style>
.ci9b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .ci9b-fig,[data-theme="dark"] .ci9b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.ci9b-fig .bg{fill:var(--bg)}
.ci9b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.ci9b-fig .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.ci9b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif}
.ci9b-fig .title{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif}
.ci9b-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif}
.ci9b-fig .ac{fill:var(--ac)}
.ci9b-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- defs -->
<defs>
<marker id="ci9b-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--ln)"/></marker>
</defs>
<!-- bg -->
<rect class="bg" x="0" y="0" width="800" height="320" rx="8"/>
<!-- title -->
<text x="400" y="26" text-anchor="middle" class="title">What one .next() on .flatten() actually does</text>
<!-- consumer -->
<rect class="box" x="300" y="44" width="200" height="34" rx="6"/>
<text x="400" y="66" text-anchor="middle" class="tx">collect() asks for an item</text>
<path class="ln" d="M400 78V100" marker-end="url(#ci9b-arrow)"/>
<!-- current inner iterator -->
<rect class="acbox" x="290" y="100" width="220" height="36" rx="6"/>
<text x="400" y="123" text-anchor="middle" class="tx ac">current inner iterator</text>
<!-- yield branch -->
<path class="ln" d="M510 118H560" marker-end="url(#ci9b-arrow)"/>
<rect class="box" x="560" y="100" width="210" height="36" rx="6"/>
<text x="665" y="123" text-anchor="middle" class="tx">has an item → yield it</text>
<!-- exhausted branch -->
<path class="ln" d="M400 136V170" marker-end="url(#ci9b-arrow)"/>
<rect class="box" x="300" y="170" width="200" height="36" rx="6"/>
<text x="400" y="193" text-anchor="middle" class="tx">inner is exhausted</text>
<path class="ln" d="M400 206V240" marker-end="url(#ci9b-arrow)"/>
<rect class="box" x="300" y="240" width="200" height="36" rx="6"/>
<text x="400" y="263" text-anchor="middle" class="tx">outer.next()</text>
<!-- done branch -->
<path class="ln" d="M500 258H560" marker-end="url(#ci9b-arrow)"/>
<rect class="box" x="560" y="240" width="210" height="36" rx="6"/>
<text x="665" y="263" text-anchor="middle" class="tx">None → flatten is done</text>
<!-- loop back -->
<path class="ln" d="M300 258H200V118H290" marker-end="url(#ci9b-arrow)"/>
<text x="190" y="190" text-anchor="end" class="mut">Some(inner_vec)</text>
<text x="190" y="206" text-anchor="end" class="mut">becomes current</text>
<!-- footer -->
<text x="400" y="302" text-anchor="middle" class="mut">No intermediate Vec is ever built for the nested layer — only collect() allocates, once, for the result.</text>
</svg>
</div>

## Benchmark Example

```rust
let nested: Vec<Vec<i32>> = (0..1_000).map(|i| vec![i; 100]).collect();

// Iterator approach
let start = std::time::Instant::now();
let flat = nested.iter().flatten().copied().collect::<Vec<_>>();
println!("flatten: {:?}", start.elapsed());

// Manual approach with pre-allocation
let start = std::time::Instant::now();
let total_len = nested.iter().map(|v| v.len()).sum();
let mut flat = Vec::with_capacity(total_len);
flat.extend(nested.into_iter().flatten());
println!("manual: {:?}", start.elapsed());
```

**Typical Result**:
- Manual with pre-allocation is ~10–20% faster for large Vecs.
- Iterator version is more concise and equally fast for small data.

## The approaches compared
| Approach | Best For | Pitfalls |
|----------|----------|----------|
| Iterator | Readability, chaining operations | Slightly slower without pre-allocation |
| Manual | Maximum performance, large data | Verbose; requires length calculation |

## Advanced: Zero-Copy Flattening

If you have `Vec<&[T]>` instead of `Vec<Vec<T>>`, use `.flatten().copied()` to avoid cloning:

```rust
let slices: Vec<&[i32]> = vec![&[1, 2], &[3, 4]];
let flat: Vec<i32> = slices.iter().flatten().copied().collect();
```

**Use .flatten() for**:
- Clean, idiomatic code.
- Chaining with other iterator adapters (e.g., `.filter()`).

**Use manual extend for**:
- Large datasets where pre-allocation matters.
- Cases where you already know the total length.

**Always pre-allocate for manual concatenation of large collections!**

To flatten and deduplicate in one pass, collect into a `HashSet` instead of a `Vec` —
`.flatten().collect::<HashSet<_>>()` — and accept that you lose the ordering.
