#no-std #enums #iterator

no-std either-both

An enum similar to the well-known Either, but with a Both variant

1 stable release

Uses new Rust 2024

1.0.0 Sep 13, 2025

#739 in Data structures

Download history 130/week @ 2025-09-12 16/week @ 2025-09-19 3/week @ 2025-09-26

149 downloads per month

MIT/Apache

61KB
953 lines

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.

Usage

It's recommended to use the prelude, which brings into scope Either<L, R>, and also its variants:

  • Left
  • Right
  • Both
use either_both::prelude::*;

// Just like how Option's and Result's variants are often used without the prefix,
// you can now use Left, Right, and Both without prefixing with "Either::"
const CHOICES: [Either<bool, u8>;3] = [Left(true), Right(1), Both(false, 0)];

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> {
        Either::from_options(self.0.next(), self.1.next())
    }
}

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)
    }
}

Dependencies