Skip to content

Commit c957b80

Browse files
committed
Update binary_search example to instead redirect to partition_point
1 parent c2afaba commit c957b80

File tree

2 files changed

+30
-4
lines changed
  • library

2 files changed

+30
-4
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -2593,14 +2593,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
25932593
/// ```
25942594
///
25952595
/// If you want to insert an item to a sorted deque, while maintaining
2596-
/// sort order:
2596+
/// sort order, consider using [`partition_point`]:
25972597
///
25982598
/// ```
25992599
/// use std::collections::VecDeque;
26002600
///
26012601
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
26022602
/// let num = 42;
2603-
/// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
2603+
/// let idx = deque.partition_point(&num);
2604+
/// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
26042605
/// deque.insert(idx, num);
26052606
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
26062607
/// ```
@@ -2744,6 +2745,19 @@ impl<T, A: Allocator> VecDeque<T, A> {
27442745
/// assert!(deque.iter().take(i).all(|&x| x < 5));
27452746
/// assert!(deque.iter().skip(i).all(|&x| !(x < 5)));
27462747
/// ```
2748+
///
2749+
/// If you want to insert an item to a sorted deque, while maintaining
2750+
/// sort order:
2751+
///
2752+
/// ```
2753+
/// use std::collections::VecDeque;
2754+
///
2755+
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
2756+
/// let num = 42;
2757+
/// let idx = deque.partition_point(&num);
2758+
/// deque.insert(idx, num);
2759+
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
2760+
/// ```
27472761
#[stable(feature = "vecdeque_binary_search", since = "1.54.0")]
27482762
pub fn partition_point<P>(&self, mut pred: P) -> usize
27492763
where

library/core/src/slice/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -2332,12 +2332,13 @@ impl<T> [T] {
23322332
/// ```
23332333
///
23342334
/// If you want to insert an item to a sorted vector, while maintaining
2335-
/// sort order:
2335+
/// sort order, consider using [`partition_point`]:
23362336
///
23372337
/// ```
23382338
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
23392339
/// let num = 42;
2340-
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
2340+
/// let idx = s.partition_point(&num);
2341+
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
23412342
/// s.insert(idx, num);
23422343
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
23432344
/// ```
@@ -3744,6 +3745,17 @@ impl<T> [T] {
37443745
/// assert!(v[..i].iter().all(|&x| x < 5));
37453746
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
37463747
/// ```
3748+
///
3749+
/// If you want to insert an item to a sorted vector, while maintaining
3750+
/// sort order:
3751+
///
3752+
/// ```
3753+
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
3754+
/// let num = 42;
3755+
/// let idx = s.partition_point(&num);
3756+
/// s.insert(idx, num);
3757+
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
3758+
/// ```
37473759
#[stable(feature = "partition_point", since = "1.52.0")]
37483760
#[must_use]
37493761
pub fn partition_point<P>(&self, mut pred: P) -> usize

0 commit comments

Comments
 (0)