@@ -2359,6 +2359,28 @@ impl<T> [T] {
2359
2359
/// assert!(match r { Ok(1..=4) => true, _ => false, });
2360
2360
/// ```
2361
2361
///
2362
+ /// If you want to find that whole *range* of matching items, rather than
2363
+ /// an arbitrary matching one, that can be done using [`partition_point`]:
2364
+ /// ```
2365
+ /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
2366
+ ///
2367
+ /// let low = s.partition_point(|x| x < &1);
2368
+ /// assert_eq!(low, 1);
2369
+ /// let high = s.partition_point(|x| x <= &1);
2370
+ /// assert_eq!(high, 5);
2371
+ /// let r = s.binary_search(&1);
2372
+ /// assert!((low..high).contains(&r.unwrap()));
2373
+ ///
2374
+ /// assert!(s[..low].iter().all(|&x| x < 1));
2375
+ /// assert!(s[low..high].iter().all(|&x| x == 1));
2376
+ /// assert!(s[high..].iter().all(|&x| x > 1));
2377
+ ///
2378
+ /// // For something not found, the "range" of equal items is empty
2379
+ /// assert_eq!(s.partition_point(|x| x < &11), 9);
2380
+ /// assert_eq!(s.partition_point(|x| x <= &11), 9);
2381
+ /// assert_eq!(s.binary_search(&11), Err(9));
2382
+ /// ```
2383
+ ///
2362
2384
/// If you want to insert an item to a sorted vector, while maintaining
2363
2385
/// sort order, consider using [`partition_point`]:
2364
2386
///
@@ -3778,6 +3800,16 @@ impl<T> [T] {
3778
3800
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
3779
3801
/// ```
3780
3802
///
3803
+ /// If all elements of the slice match the predicate, including if the slice
3804
+ /// is empty, then the length of the slice will be returned:
3805
+ ///
3806
+ /// ```
3807
+ /// let a = [2, 4, 8];
3808
+ /// assert_eq!(a.partition_point(|x| x < &100), a.len());
3809
+ /// let a: [i32; 0] = [];
3810
+ /// assert_eq!(a.partition_point(|x| x < &100), 0);
3811
+ /// ```
3812
+ ///
3781
3813
/// If you want to insert an item to a sorted vector, while maintaining
3782
3814
/// sort order:
3783
3815
///
0 commit comments