Skip to content

Commit b5ba2f5

Browse files
committed
auto merge of #17820 : pnkfelix/rust/fsk-improve-binary_search-doc2, r=alexcrichton
Add example to doc for `slice::ImmutableSlice::binary_search`. Fix #17817.
2 parents 77a4f37 + d1e0f72 commit b5ba2f5

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/libcore/slice.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,32 @@ pub trait ImmutableSlice<'a, T> {
223223
/// order code that indicates whether its argument is `Less`,
224224
/// `Equal` or `Greater` the desired target.
225225
///
226-
/// If the value is found then `Found` is returned, containing the
227-
/// index of the matching element; if the value is not found then
226+
/// If a matching value is found then returns `Found`, containing
227+
/// the index for the matched element; if no match is found then
228228
/// `NotFound` is returned, containing the index where a matching
229229
/// element could be inserted while maintaining sorted order.
230+
///
231+
/// # Example
232+
///
233+
/// Looks up a series of four elements. The first is found, with a
234+
/// uniquely determined position; the second and third are not
235+
/// found; the fourth could match any position in `[1,4]`.
236+
///
237+
/// ```rust
238+
/// use std::slice::{Found, NotFound};
239+
/// let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
240+
/// let s = s.as_slice();
241+
///
242+
/// let seek = 13;
243+
/// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), Found(9));
244+
/// let seek = 4;
245+
/// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), NotFound(7));
246+
/// let seek = 100;
247+
/// assert_eq!(s.binary_search(|probe| probe.cmp(&seek)), NotFound(13));
248+
/// let seek = 1;
249+
/// let r = s.binary_search(|probe| probe.cmp(&seek));
250+
/// assert!(match r { Found(1...4) => true, _ => false, });
251+
/// ```
230252
#[unstable = "waiting on unboxed closures"]
231253
fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult;
232254

@@ -1043,6 +1065,24 @@ pub trait ImmutableOrdSlice<T: Ord> {
10431065
/// index of the matching element; if the value is not found then
10441066
/// `NotFound` is returned, containing the index where a matching
10451067
/// element could be inserted while maintaining sorted order.
1068+
///
1069+
/// # Example
1070+
///
1071+
/// Looks up a series of four elements. The first is found, with a
1072+
/// uniquely determined position; the second and third are not
1073+
/// found; the fourth could match any position in `[1,4]`.
1074+
///
1075+
/// ```rust
1076+
/// use std::slice::{Found, NotFound};
1077+
/// let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
1078+
/// let s = s.as_slice();
1079+
///
1080+
/// assert_eq!(s.binary_search_elem(&13), Found(9));
1081+
/// assert_eq!(s.binary_search_elem(&4), NotFound(7));
1082+
/// assert_eq!(s.binary_search_elem(&100), NotFound(13));
1083+
/// let r = s.binary_search_elem(&1);
1084+
/// assert!(match r { Found(1...4) => true, _ => false, });
1085+
/// ```
10461086
#[unstable = "name likely to change"]
10471087
fn binary_search_elem(&self, x: &T) -> BinarySearchResult;
10481088
}

0 commit comments

Comments
 (0)