Skip to content

Commit 1186228

Browse files
authored
Rollup merge of rust-lang#95743 - yaahc:binary-search-clarification, r=Mark-Simulacrum
Update binary_search example to instead redirect to partition_point Inspired by discussion in the tracking issue for `Result::into_ok_or_err`: rust-lang#82223 (comment) People are surprised by us not providing a `Result<T, T> -> T` conversion, and the main culprit for this confusion seems to be the `binary_search` API. We should instead redirect people to the equivalent API that implicitly does that `Result<T, T> -> T` conversion internally which should obviate the need for the `into_ok_or_err` function and give us time to work towards a more general solution that applies to all enums rather than just `Result` such as making or_patterns usable for situations like this via postfix `match`. I choose to duplicate the example rather than simply moving it from `binary_search` to partition point because most of the confusion seems to arise when people are looking at `binary_search`. It makes sense to me to have the example presented immediately rather than requiring people to click through to even realize there is an example. If I had to put it in only one place I'd leave it in `binary_search` and remove it from `partition_point` but it seems pretty obviously relevant to `partition_point` so I figured the best option would be to duplicate it.
2 parents d12b857 + 0eb0d89 commit 1186228

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(|&x| x < 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(|&x| x < 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
@@ -2331,12 +2331,13 @@ impl<T> [T] {
23312331
/// ```
23322332
///
23332333
/// If you want to insert an item to a sorted vector, while maintaining
2334-
/// sort order:
2334+
/// sort order, consider using [`partition_point`]:
23352335
///
23362336
/// ```
23372337
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
23382338
/// let num = 42;
2339-
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
2339+
/// let idx = s.partition_point(|&x| x < num);
2340+
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
23402341
/// s.insert(idx, num);
23412342
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
23422343
/// ```
@@ -3743,6 +3744,17 @@ impl<T> [T] {
37433744
/// assert!(v[..i].iter().all(|&x| x < 5));
37443745
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
37453746
/// ```
3747+
///
3748+
/// If you want to insert an item to a sorted vector, while maintaining
3749+
/// sort order:
3750+
///
3751+
/// ```
3752+
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
3753+
/// let num = 42;
3754+
/// let idx = s.partition_point(|&x| x < num);
3755+
/// s.insert(idx, num);
3756+
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
3757+
/// ```
37463758
#[stable(feature = "partition_point", since = "1.52.0")]
37473759
#[must_use]
37483760
pub fn partition_point<P>(&self, mut pred: P) -> usize

0 commit comments

Comments
 (0)