Skip to content

Commit 37ed3ce

Browse files
committed
enhanced documentation of binary search methods for slice and VecDeque for unsorted instances
1 parent 159ba8a commit 37ed3ce

File tree

2 files changed

+20
-14
lines changed
  • library

2 files changed

+20
-14
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
23782378
}
23792379

23802380
/// Binary searches this `VecDeque` for a given element.
2381-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
2381+
/// If the `VecDeque` is not sorted, the returned result is unspecified and
2382+
/// meaningless.
23822383
///
23832384
/// If the value is found then [`Result::Ok`] is returned, containing the
23842385
/// index of the matching element. If there are multiple matches, then any
@@ -2434,12 +2435,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
24342435
}
24352436

24362437
/// Binary searches this `VecDeque` with a comparator function.
2437-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
24382438
///
2439-
/// The comparator function should implement an order consistent
2440-
/// with the sort order of the deque, returning an order code that
2441-
/// indicates whether its argument is `Less`, `Equal` or `Greater`
2442-
/// than the desired target.
2439+
/// The comparator function should return an order code that indicates
2440+
/// whether its argument is `Less`, `Equal` or `Greater` the desired
2441+
/// target.
2442+
/// If the `VecDeque` is not sorted or if the comparator function does not
2443+
/// implement an order consistent with the sort order of the underlying
2444+
/// `VecDeque`, the returned result is unspecified and meaningless.
24432445
///
24442446
/// If the value is found then [`Result::Ok`] is returned, containing the
24452447
/// index of the matching element. If there are multiple matches, then any
@@ -2489,10 +2491,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
24892491
}
24902492

24912493
/// Binary searches this `VecDeque` with a key extraction function.
2492-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
24932494
///
24942495
/// Assumes that the deque is sorted by the key, for instance with
24952496
/// [`make_contiguous().sort_by_key()`] using the same key extraction function.
2497+
/// If the deque is not sorted by the key, the returned result is
2498+
/// unspecified and meaningless.
24962499
///
24972500
/// If the value is found then [`Result::Ok`] is returned, containing the
24982501
/// index of the matching element. If there are multiple matches, then any

library/core/src/slice/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,8 @@ impl<T> [T] {
23432343
}
23442344

23452345
/// Binary searches this slice for a given element.
2346-
/// This behaves similarly to [`contains`] if this slice is sorted.
2346+
/// If the slice is not sorted, the returned result is unspecified and
2347+
/// meaningless.
23472348
///
23482349
/// If the value is found then [`Result::Ok`] is returned, containing the
23492350
/// index of the matching element. If there are multiple matches, then any
@@ -2418,12 +2419,13 @@ impl<T> [T] {
24182419
}
24192420

24202421
/// Binary searches this slice with a comparator function.
2421-
/// This behaves similarly to [`contains`] if this slice is sorted.
24222422
///
2423-
/// The comparator function should implement an order consistent
2424-
/// with the sort order of the underlying slice, returning an
2425-
/// order code that indicates whether its argument is `Less`,
2426-
/// `Equal` or `Greater` the desired target.
2423+
/// The comparator function should return an order code that indicates
2424+
/// whether its argument is `Less`, `Equal` or `Greater` the desired
2425+
/// target.
2426+
/// If the slice is not sorted or if the comparator function does not
2427+
/// implement an order consistent with the sort order of the underlying
2428+
/// slice, the returned result is unspecified and meaningless.
24272429
///
24282430
/// If the value is found then [`Result::Ok`] is returned, containing the
24292431
/// index of the matching element. If there are multiple matches, then any
@@ -2504,10 +2506,11 @@ impl<T> [T] {
25042506
}
25052507

25062508
/// Binary searches this slice with a key extraction function.
2507-
/// This behaves similarly to [`contains`] if this slice is sorted.
25082509
///
25092510
/// Assumes that the slice is sorted by the key, for instance with
25102511
/// [`sort_by_key`] using the same key extraction function.
2512+
/// If the slice is not sorted by the key, the returned result is
2513+
/// unspecified and meaningless.
25112514
///
25122515
/// If the value is found then [`Result::Ok`] is returned, containing the
25132516
/// index of the matching element. If there are multiple matches, then any

0 commit comments

Comments
 (0)