Skip to content

Commit

Permalink
Updated Binary Search docs to show how to insert into an already sort…
Browse files Browse the repository at this point in the history
…ed list

Resolves: rust-lang#61684

Apply suggestions from code review

Co-Authored-By: Jonas Schievink <jonasschievink@gmail.com>

Cleanup: Fixed failing tidy checks
  • Loading branch information
Philip Tallo committed Jun 12, 2019
1 parent a73ecb3 commit ba21a43
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,23 @@ impl<T> [T] {
/// let r = s.binary_search(&1);
/// assert!(match r { Ok(1..=4) => true, _ => false, });
/// ```
///
/// Inserts a new element into already sorted array while maintaining sorted
/// order. Then asserts that the item was inserted correctly.
///
/// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 14;
///
/// match s.binary_search(&num) {
/// Ok(_) => {}
/// Err(pos) => {
/// s.insert(pos, num);
/// }
/// };
///
/// assert_eq!(s[10], num)
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
where T: Ord
Expand Down

0 comments on commit ba21a43

Please sign in to comment.