#232
Jun 30, 2026
232. Iterator::partition — Split Into Two Collections in One Pass
Splitting items into “matches” and “the rest” with two .filter() calls walks the list twice. partition does it in a single pass.
The two-filter version reads fine but iterates the whole thing twice, once per predicate:
| |
The manual loop is one pass but needs two mutable Vecs and the bookkeeping to feed them. partition is the one-pass version with none of the noise — true items go left, false items go right:
| |
The collection types come from the annotation, and they don’t have to be Vec or even match each other — any pair that implements FromIterator works:
| |
Whenever you catch yourself filtering the same iterator twice with opposite conditions, that’s a partition.