diff options
| author | bors <bors@rust-lang.org> | 2014-10-06 20:27:14 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-06 20:27:14 +0000 |
| commit | b5ba2f5517b1f90d07969ca3facdf5132e42436c (patch) | |
| tree | d3dc61b14215c87a634f62cb7a6b6f5c60f82d02 | |
| parent | 77a4f37246c61ff840b75cbc1b6dade6d3ef13d5 (diff) | |
| parent | d1e0f7259e2f6dfc5754520730244f4e8b5248a5 (diff) | |
| download | rust-b5ba2f5517b1f90d07969ca3facdf5132e42436c.tar.gz rust-b5ba2f5517b1f90d07969ca3facdf5132e42436c.zip | |
auto merge of #17820 : pnkfelix/rust/fsk-improve-binary_search-doc2, r=alexcrichton
Add example to doc for `slice::ImmutableSlice::binary_search`. Fix #17817.
| -rw-r--r-- | src/libcore/slice.rs | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 9f925f9d371..c7ffd8f5617 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -223,10 +223,32 @@ pub trait ImmutableSlice<'a, T> { /// order code that indicates whether its argument is `Less`, /// `Equal` or `Greater` the desired target. /// - /// If the value is found then `Found` is returned, containing the - /// index of the matching element; if the value is not found then + /// If a matching value is found then returns `Found`, containing + /// the index for the matched element; if no match is found then /// `NotFound` is returned, containing the index where a matching /// element could be inserted while maintaining sorted order. + /// + /// # Example + /// + /// Looks up a series of four elements. The first is found, with a + /// uniquely determined position; the second and third are not + /// found; the fourth could match any position in `[1,4]`. + /// + /// ```rust + /// use std::slice::{Found, NotFound}; + /// let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; + /// let s = s.as_slice(); + /// + /// let seek = 13; + /// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), Found(9)); + /// let seek = 4; + /// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), NotFound(7)); + /// let seek = 100; + /// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), NotFound(13)); + /// let seek = 1; + /// let r = s.binary_search(|probe| probe.cmp(&seek)); + /// assert!(match r { Found(1...4) => true, _ => false, }); + /// ``` #[unstable = "waiting on unboxed closures"] fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult; @@ -1043,6 +1065,24 @@ pub trait ImmutableOrdSlice<T: Ord> { /// index of the matching element; if the value is not found then /// `NotFound` is returned, containing the index where a matching /// element could be inserted while maintaining sorted order. + /// + /// # Example + /// + /// Looks up a series of four elements. The first is found, with a + /// uniquely determined position; the second and third are not + /// found; the fourth could match any position in `[1,4]`. + /// + /// ```rust + /// use std::slice::{Found, NotFound}; + /// let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; + /// let s = s.as_slice(); + /// + /// assert_eq!(s.binary_search_elem(&13), Found(9)); + /// assert_eq!(s.binary_search_elem(&4), NotFound(7)); + /// assert_eq!(s.binary_search_elem(&100), NotFound(13)); + /// let r = s.binary_search_elem(&1); + /// assert!(match r { Found(1...4) => true, _ => false, }); + /// ``` #[unstable = "name likely to change"] fn binary_search_elem(&self, x: &T) -> BinarySearchResult; } |
