improve example on inserting to a sorted vector to avoid shifting equal elements

This commit is contained in:
Andy Kurnia 2024-03-23 21:38:32 +08:00
parent c3b05c6e5b
commit 5afe4a9e09
2 changed files with 3 additions and 3 deletions

View File

@ -2464,7 +2464,7 @@ unsafe fn rotate_right_inner(&mut self, k: usize) {
/// ///
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into(); /// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let num = 42; /// let num = 42;
/// let idx = deque.partition_point(|&x| x < num); /// let idx = deque.partition_point(|&x| x <= num);
/// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);` /// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
/// deque.insert(idx, num); /// deque.insert(idx, num);
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);

View File

@ -2728,7 +2728,7 @@ pub fn strip_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> Op
/// ``` /// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; /// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 42; /// let num = 42;
/// let idx = s.partition_point(|&x| x < num); /// let idx = s.partition_point(|&x| x <= num);
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);` /// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
/// s.insert(idx, num); /// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
@ -4179,7 +4179,7 @@ pub fn is_sorted_by_key<'a, F, K>(&'a self, f: F) -> bool
/// ``` /// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; /// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 42; /// let num = 42;
/// let idx = s.partition_point(|&x| x < num); /// let idx = s.partition_point(|&x| x <= num);
/// s.insert(idx, num); /// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ``` /// ```