Skip to content

Commit

Permalink
fix doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
pickx committed Jun 26, 2023
1 parent c798a76 commit dd1bf32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/at_most_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::mem;
/// elements in the iterator.
///
/// See [`.at_most_one()`](crate::Itertools::at_most_one) for more detail.
#[derive(PartialEq, Eq, Clone)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum AtMostOneResult<I: Iterator> {
/// The iterator was empty and therefore had zero elements.
Zero,
Expand All @@ -15,11 +15,11 @@ pub enum AtMostOneResult<I: Iterator> {
One(I::Item),

/// The iterator had more than one element.
/// `MoreThanOne` is an iterator which yields the same elements as the original iterator.
/// [`MoreThanOne`](crate::MoreThanOne) is an iterator which yields the same elements as the original iterator.
MoreThanOne(MoreThanOne<I>),
}

#[derive(PartialEq, Eq, Clone)]
#[derive(PartialEq, Eq, Clone, Debug)]
enum IterSource<T> {
FirstElement(T, T),
SecondElement(T),
Expand All @@ -28,7 +28,7 @@ enum IterSource<T> {

/// The iterator returned by [`.at_most_one()`](crate::Itertools::at_most_one), if the original iterator
/// had at least two elements remaining. Yields the same elements as the original iterator.
#[derive(PartialEq, Eq, Clone)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct MoreThanOne<I: Iterator> {
next_source: IterSource<I::Item>,
inner: I,
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3699,12 +3699,13 @@ pub trait Itertools : Iterator {
///
/// This function is especially useful in `match` statements, to clearly assert the number of elements
/// in an iterator or take different paths depending on this number.
/// For example, using [`.find()`](std::iter::Iterator::find) will stop at the first element,
/// For example, using [`.find()`](std::iter::Iterator::find) will stop at the first matched element,
/// but you may additionally want to validate that no more than a single element satisfies the predicate.
///
/// # Examples
/// ```
/// use itertools::Itertools;
/// use itertools::{Itertools, AtMostOneResult};
/// use core::iter::{empty, once};
///
/// let numbers = [-9, -1, -7, 4, -38, -21].iter().copied();
/// let positive_number: Option<i32> = match numbers.filter(|&num| num >= 0).at_most_one() {
Expand All @@ -3714,10 +3715,11 @@ pub trait Itertools : Iterator {
/// };
/// assert_eq!(positive_number, Some(4));
///
/// let zero_elements = empty().at_most_one();
/// assert_eq!(zero_elements, AtMostOneResult::Zero);
/// let zero_elements = empty::<i32>().at_most_one();
/// assert!(matches!(zero_elements, AtMostOneResult::Zero));
///
/// assert_eq!(one_element, AtMostOneResult::One(5));
/// let one_element = once(5).at_most_one();
/// assert!(matches!(one_element, AtMostOneResult::One(5)));
///
/// let many_elements = (1..=10).at_most_one();
/// let AtMostOneResult::MoreThanOne(more_than_one) = many_elements else { panic!() };
Expand Down

0 comments on commit dd1bf32

Please sign in to comment.