Skip to content

Commit 4690a0f

Browse files
committed
Add another example for take_any_while
1 parent 68457b0 commit 4690a0f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/iter/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,25 @@ pub trait ParallelIterator: Sized + Send {
22792279
/// assert!(result.len() <= 50);
22802280
/// assert!(result.windows(2).all(|w| w[0] < w[1]));
22812281
/// ```
2282+
///
2283+
/// ```
2284+
/// use rayon::prelude::*;
2285+
/// use std::sync::atomic::AtomicUsize;
2286+
/// use std::sync::atomic::Ordering::Relaxed;
2287+
///
2288+
/// // Collect any group of items that sum <= 1000
2289+
/// let quota = AtomicUsize::new(1000);
2290+
/// let result: Vec<_> = (0_usize..100)
2291+
/// .into_par_iter()
2292+
/// .take_any_while(|&x| {
2293+
/// quota.fetch_update(Relaxed, Relaxed, |q| q.checked_sub(x))
2294+
/// .is_ok()
2295+
/// })
2296+
/// .collect();
2297+
///
2298+
/// let sum = result.iter().sum::<usize>();
2299+
/// assert!(matches!(sum, 902..=1000));
2300+
/// ```
22822301
fn take_any_while<P>(self, predicate: P) -> TakeAnyWhile<Self, P>
22832302
where
22842303
P: Fn(&Self::Item) -> bool + Sync + Send,

0 commit comments

Comments
 (0)