We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
ZipLongest::fold
try_fold
1 parent 98344ad commit 6c588cdCopy full SHA for 6c588cd
src/zip_longest.rs
@@ -54,17 +54,20 @@ where
54
}
55
56
#[inline]
57
- fn fold<B, F>(self, mut init: B, mut f: F) -> B
+ fn fold<B, F>(self, init: B, mut f: F) -> B
58
where
59
Self: Sized,
60
F: FnMut(B, Self::Item) -> B,
61
{
62
- let Self { a, mut b } = self;
63
- init = a.fold(init, |init, a| match b.next() {
64
- Some(b) => f(init, EitherOrBoth::Both(a, b)),
65
- None => f(init, EitherOrBoth::Left(a)),
+ let Self { mut a, mut b } = self;
+ let res = a.try_fold(init, |init, a| match b.next() {
+ Some(b) => Ok(f(init, EitherOrBoth::Both(a, b))),
+ None => Err(f(init, EitherOrBoth::Left(a))),
66
});
67
- b.fold(init, |init, b| f(init, EitherOrBoth::Right(b)))
+ match res {
68
+ Ok(acc) => b.map(EitherOrBoth::Right).fold(acc, f),
69
+ Err(acc) => a.map(EitherOrBoth::Left).fold(acc, f),
70
+ }
71
72
73
0 commit comments