mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 01:04:03 +00:00
Rollup merge of #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`: https://github.com/rust-lang/rust/issues/82223#issuecomment-1067098167 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.
This commit is contained in:
commit
e25bc303f1
@ -2593,14 +2593,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted deque, while maintaining
|
||||
/// sort order:
|
||||
/// sort order, consider using [`partition_point`]:
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecDeque;
|
||||
///
|
||||
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
|
||||
/// let num = 42;
|
||||
/// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
|
||||
/// let idx = deque.partition_point(|&x| x < num);
|
||||
/// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
|
||||
/// deque.insert(idx, num);
|
||||
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
@ -2744,6 +2745,19 @@ impl<T, A: Allocator> VecDeque<T, A> {
|
||||
/// assert!(deque.iter().take(i).all(|&x| x < 5));
|
||||
/// assert!(deque.iter().skip(i).all(|&x| !(x < 5)));
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted deque, while maintaining
|
||||
/// sort order:
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecDeque;
|
||||
///
|
||||
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
|
||||
/// let num = 42;
|
||||
/// let idx = deque.partition_point(|&x| x < num);
|
||||
/// deque.insert(idx, num);
|
||||
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
#[stable(feature = "vecdeque_binary_search", since = "1.54.0")]
|
||||
pub fn partition_point<P>(&self, mut pred: P) -> usize
|
||||
where
|
||||
|
@ -2331,12 +2331,13 @@ impl<T> [T] {
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted vector, while maintaining
|
||||
/// sort order:
|
||||
/// sort order, consider using [`partition_point`]:
|
||||
///
|
||||
/// ```
|
||||
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
|
||||
/// let num = 42;
|
||||
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
|
||||
/// let idx = s.partition_point(|&x| x < num);
|
||||
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
|
||||
/// s.insert(idx, num);
|
||||
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
@ -3743,6 +3744,17 @@ impl<T> [T] {
|
||||
/// assert!(v[..i].iter().all(|&x| x < 5));
|
||||
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted vector, while maintaining
|
||||
/// sort order:
|
||||
///
|
||||
/// ```
|
||||
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
|
||||
/// let num = 42;
|
||||
/// let idx = s.partition_point(|&x| x < num);
|
||||
/// s.insert(idx, num);
|
||||
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
#[stable(feature = "partition_point", since = "1.52.0")]
|
||||
#[must_use]
|
||||
pub fn partition_point<P>(&self, mut pred: P) -> usize
|
||||
|
Loading…
Reference in New Issue
Block a user