about summary refs log tree commit diff
path: root/library/alloc/src/collections/vec_deque
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-02 03:14:05 +0000
committerbors <bors@rust-lang.org>2024-04-02 03:14:05 +0000
commit6bbd8c519af69ebc30486d6cf02b7f7a52113950 (patch)
tree33715fa5c1fa2470d26253e0874cd471223896e8 /library/alloc/src/collections/vec_deque
parent31075bb493f49c2b739ff20b2e96a9045e1eba13 (diff)
parent643029693b2987b4b7c8b5073e8a25536cca7069 (diff)
downloadrust-6bbd8c519af69ebc30486d6cf02b7f7a52113950.tar.gz
rust-6bbd8c519af69ebc30486d6cf02b7f7a52113950.zip
Auto merge of #122945 - andy-k:sorted-vec-example, r=jhpratt
improve example on inserting to a sorted vector to avoid shifting equal elements
Diffstat (limited to 'library/alloc/src/collections/vec_deque')
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs6
1 files changed, 4 insertions, 2 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index 5ff7f184a6d..e2fc320f280 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -2464,8 +2464,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
     ///
     /// 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);
-    /// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
+    /// let idx = deque.partition_point(|&x| x <= num);
+    /// // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to
+    /// // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` may allow `insert`
+    /// // to shift less elements.
     /// deque.insert(idx, num);
     /// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
     /// ```