Rollup merge of #63442 - lzutao:vec-bin-search-insert, r=Mark-Simulacrum

Add an example to show how to insert item to a sorted vec

Closes #61684
cc #61742
r? @Mark-Simulacrum, @jonas-schievink
This commit is contained in:
Mark Rousskov 2019-08-11 15:18:46 -04:00 committed by GitHub
commit e16b12fd49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1364,6 +1364,17 @@ impl<T> [T] {
/// let r = s.binary_search(&1);
/// assert!(match r { Ok(1..=4) => true, _ => false, });
/// ```
///
/// If you want to insert an item to a sorted vector, while maintaining
/// sort order:
///
/// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 42;
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
/// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
where T: Ord