@@ -223,10 +223,32 @@ pub trait ImmutableSlice<'a, T> {
223
223
/// order code that indicates whether its argument is `Less`,
224
224
/// `Equal` or `Greater` the desired target.
225
225
///
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
228
228
/// `NotFound` is returned, containing the index where a matching
229
229
/// 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
+ /// ```
230
252
#[ unstable = "waiting on unboxed closures" ]
231
253
fn binary_search ( & self , f: |& T | -> Ordering ) -> BinarySearchResult ;
232
254
@@ -1043,6 +1065,24 @@ pub trait ImmutableOrdSlice<T: Ord> {
1043
1065
/// index of the matching element; if the value is not found then
1044
1066
/// `NotFound` is returned, containing the index where a matching
1045
1067
/// 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
+ /// ```
1046
1086
#[ unstable = "name likely to change" ]
1047
1087
fn binary_search_elem ( & self , x : & T ) -> BinarySearchResult ;
1048
1088
}
0 commit comments