either-both

Crates.ioeither-both
lib.rseither-both
version1.0.0
created_at2025-09-13 16:10:21.529285+00
updated_at2025-09-13 16:10:21.529285+00
descriptionAn enum similar to the well-known `Either`, but with a `Both` variant
homepage
repositoryhttps://github.com/spenserblack/either-both
max_upload_size
id1837827
size80,678
Spenser Black (spenserblack)

documentation

README

either-both

This crate is intended to be similar to either, but also cover the "both" variant.

This crate was inspired by wanting to implement a method similar to [Iterator::zip] that continues until both zipped iterators are exhausted.

A common use case could be replacing the type (Option<L>, Option<R>) where the (None, None) variant is impossible.

Example

use either_both::prelude::*;

pub struct ZipToEnd<A: Iterator, B: Iterator>(A, B);

impl<A: Iterator, B: Iterator> Iterator for ZipToEnd<A, B> {
    type Item = Either<<A as Iterator>::Item, <B as Iterator>::Item>;

    fn next(&mut self) -> Option<Self::Item> {
        let either = match (self.0.next(), self.1.next()) {
            (Some(a), Some(b)) => Both(a, b),
            (Some(a), None) => Left(a),
            (None, Some(b)) => Right(b),
            (None, None) => return None,
        };
        Some(either)
    }
}
Commit count: 8

cargo fmt