diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-04-11 12:06:52 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-11 12:06:52 +0200 |
| commit | e25bc303f1ba73b9ed1f83573bcb573f9c268c9f (patch) | |
| tree | 5acdbc059fb5fda8c7e23519b48edcba5850895c /library/alloc | |
| parent | 48a9e104dfafdec3a5d00d365229d6369939f433 (diff) | |
| parent | 0eb0d891ad2558f523527db8595c1daf03c5ca83 (diff) | |
| download | rust-e25bc303f1ba73b9ed1f83573bcb573f9c268c9f.tar.gz rust-e25bc303f1ba73b9ed1f83573bcb573f9c268c9f.zip | |
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.
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/collections/vec_deque/mod.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index dcae58ae590..488671d8d8d 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -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 |
