std-method

Iterator::unzip — split pairs into collections

Consume pairs into two collections, one for each component. The collection types must be inferred or specified. Both outputs preserve item order when they are vectors. Expected stdout (not executed): ["Ada", "Lin"] [8, 9] Common mistake: Assuming unzip works on triples or that it returns borrowed views.

Formal Syntax & Structure

rust syntaxrust
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where Self: Sized + Iterator<Item = (A, B)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>

Working Example

Example Snippetrust
fn main() {    let (names, scores): (Vec<_>, Vec<_>) = [("Ada", 8), ("Lin", 9)].into_iter().unzip();    println!("{names:?} {scores:?}");}
Rust Reference