File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments