Implement zip on pair of optionals

Hello, what you think about this

impl<A, B> (Option<A>, Option<B>) {
    fn zip(&self) -> Option<(&A, &B)> {
        self.0.as_ref().zip(self.1.as_ref())
    }
}

This is an alternative syntax for Option::zip for situation where you have a variable that represents a pair of Optionals and you want to combine them without doing

let (a, b) = pair;
let Some((l, r)) = a.zip(b) else { ... };

instead one could just do

let Some((l, r)) = pair.zip() else { ... };

And not just for Option, but for all iterables and for n-tuples with n>2 for that matter!

There would be a satisfying symmetry (a homomorphism?) if (a, b, c).zip() yielded (x, y, z). Though it would require macro-generated boilerplate like all trait impls for n-tuples.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.