std-method

Option::zip / unzip

Combine two present values into a pair; if either is missing the pair is absent. unzip separates an optional pair into two optional values. Expected stdout (not executed): Some((4, 9)) (Some(4), Some(9)) Common mistake: Expecting zip to preserve one present value when the other is missing.

Formal Syntax & Structure

rust syntaxrust
fn zip<U>(self, other: Option<U>) -> Option<(T, U)>; Option<(T, U)>::unzip(self) -> (Option<T>, Option<U>)

Working Example

Example Snippetrust
fn main() {    let pair = Some(4).zip(Some(9));    println!("{pair:?}");    println!("{:?}", pair.unzip());}
Rust Reference