Skip to content

Commit

Permalink
Add note about Result to find_or_last and find_or_first
Browse files Browse the repository at this point in the history
See GH-983
  • Loading branch information
axelkar committed Aug 7, 2024
1 parent a4a82e4 commit a781f88
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,21 @@ pub trait Itertools: Iterator {
/// assert_eq!(numbers.iter().find_or_last(|&&x| x > 2), Some(&3));
/// assert_eq!(std::iter::empty::<i32>().find_or_last(|&x| x > 5), None);
/// ```
///
/// An iterator of [`Result`]s can find the first `Ok` or the last `Err`:
///
/// ```
/// use itertools::Itertools;
///
/// let input = vec![Err(()), Ok(11), Err(()), Ok(22)];
/// assert_eq!(input.into_iter().find_or_last(|result| result.is_ok()), Some(Ok(11)));
///
/// let input: Vec<Result<(), i32>> = vec![Err(11), Err(22)];
/// assert_eq!(input.into_iter().find_or_last(|result| result.is_ok()), Some(Err(22)));
///
/// let input: Vec<Result<(), i32>> = vec![];
/// assert_eq!(input.iter().find_or_last(|result| result.is_ok()), None);
/// ```
fn find_or_last<P>(mut self, mut predicate: P) -> Option<Self::Item>
where
Self: Sized,
Expand Down Expand Up @@ -2028,6 +2043,21 @@ pub trait Itertools: Iterator {
/// assert_eq!(numbers.iter().find_or_first(|&&x| x > 2), Some(&3));
/// assert_eq!(std::iter::empty::<i32>().find_or_first(|&x| x > 5), None);
/// ```
///
/// An iterator of [`Result`]s can find the first `Ok` or the first `Err`:
///
/// ```
/// use itertools::Itertools;
///
/// let input = vec![Err(()), Ok(11), Err(()), Ok(22)];
/// assert_eq!(input.into_iter().find_or_first(|result| result.is_ok()), Some(Ok(11)));
///
/// let input: Vec<Result<(), i32>> = vec![Err(11), Err(22)];
/// assert_eq!(input.into_iter().find_or_first(|result| result.is_ok()), Some(Err(11)));
///
/// let input: Vec<Result<(), i32>> = vec![];
/// assert_eq!(input.iter().find_or_first(|result| result.is_ok()), None);
/// ```
fn find_or_first<P>(mut self, mut predicate: P) -> Option<Self::Item>
where
Self: Sized,
Expand Down

0 comments on commit a781f88

Please sign in to comment.