Skip to content

Commit

Permalink
set_from: use try_fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Feb 28, 2024
1 parent 3918da2 commit 37d47f6
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,15 +2218,15 @@ pub trait Itertools: Iterator {
Self: Iterator<Item = &'a mut A>,
J: IntoIterator<Item = A>,
{
let mut count = 0;
for elt in from {
match self.next() {
None => break,
Some(ptr) => *ptr = elt,
}
count += 1;
}
count
from.into_iter()
.try_fold(0, |count, elt| match self.next() {
Some(ptr) => {
*ptr = elt;
Ok(count + 1)
}
None => Err(count),
})
.unwrap_or_else(|count| count)
}

/// Combine all iterator elements into one String, separated by `sep`.
Expand Down

0 comments on commit 37d47f6

Please sign in to comment.