Auto merge of #122945 - andy-k:sorted-vec-example, r=jhpratt

improve example on inserting to a sorted vector to avoid shifting equal elements
This commit is contained in:
bors 2024-04-02 03:14:05 +00:00
commit 6bbd8c519a
2 changed files with 9 additions and 5 deletions

View File

@ -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]);
/// ```

View File

@ -2724,8 +2724,10 @@ impl<T> [T] {
/// ```
/// 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);
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
/// let idx = s.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 `<=` will allow `insert`
/// // to shift less elements.
/// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
@ -4175,7 +4177,7 @@ impl<T> [T] {
/// ```
/// 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);
/// 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]);
/// ```