Skip to content

Commit

Permalink
Rewrite the map_either_with example
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jul 21, 2023
1 parent 4c60855 commit 5361d36
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,19 @@ impl<L, R> Either<L, R> {
/// ```
/// use either::*;
///
/// let mut acc: Vec<String> = Vec::new();
/// let mut sum = 0;
///
/// let f = |acc: Vec<String>, s: String| acc.push(s);
/// let g = |acc: Vec<String>, u: u32| acc.push(u.to_string());
/// // Both closures want to update the same value, so pass it as context.
/// let mut f = |sum: &mut usize, s: String| { *sum += s.len(); s.to_uppercase() };
/// let mut g = |sum: &mut usize, u: usize| { *sum += u; u.to_string() };
///
/// let values: Vec<Either<String, u32>> = vec![Left("loopy".into()), Right(42)];
/// let left: Either<String, usize> = Left("loopy".into());
/// assert_eq!(left.map_either_with(&mut sum, &mut f, &mut g), Left("LOOPY".into()));
///
/// let _ = values.iter().for_each(|e| e.map_either_with(acc, f, g));
/// assert_eq!(acc, vec!["loopy".into(), "42".into()]);
/// let right: Either<String, usize> = Right(42);
/// assert_eq!(right.map_either_with(&mut sum, &mut f, &mut g), Right("42".into()));
///
/// assert_eq!(sum, 47);
/// ```
pub fn map_either_with<Ctx, F, G, M, S>(self, ctx: Ctx, f: F, g: G) -> Either<M, S>
where
Expand Down

0 comments on commit 5361d36

Please sign in to comment.