Skip to content

Commit d6f2740

Browse files
authored
Rollup merge of #106985 - jofas:106746-fix, r=ChrisDenton
Enhanced doucmentation of binary search methods for `slice` and `VecDeque` for unsorted instances Fixes #106746. Issue #106746 raises the concern that the binary search methods for slices and deques aren't explicit enough about the fact that they are only applicable to sorted slices/deques. I changed the explanation for these methods. I took the relatively harsh description of the behaviour of binary search on unsorted collections ("unspecified and meaningless") from the description of the [`partition_point`](https://doc.rust-lang.org/std/primitive.slice.html#method.partition_point) method: > If this slice is not partitioned, the returned result is unspecified and meaningless, as this method performs a kind of binary search.
2 parents f2d9a3d + b085007 commit d6f2740

File tree

2 files changed

+20
-20
lines changed
  • library

2 files changed

+20
-20
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
23942394
}
23952395

23962396
/// Binary searches this `VecDeque` for a given element.
2397-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
2397+
/// If the `VecDeque` is not sorted, the returned result is unspecified and
2398+
/// meaningless.
23982399
///
23992400
/// If the value is found then [`Result::Ok`] is returned, containing the
24002401
/// index of the matching element. If there are multiple matches, then any
@@ -2404,7 +2405,6 @@ impl<T, A: Allocator> VecDeque<T, A> {
24042405
///
24052406
/// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
24062407
///
2407-
/// [`contains`]: VecDeque::contains
24082408
/// [`binary_search_by`]: VecDeque::binary_search_by
24092409
/// [`binary_search_by_key`]: VecDeque::binary_search_by_key
24102410
/// [`partition_point`]: VecDeque::partition_point
@@ -2450,12 +2450,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
24502450
}
24512451

24522452
/// Binary searches this `VecDeque` with a comparator function.
2453-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
24542453
///
2455-
/// The comparator function should implement an order consistent
2456-
/// with the sort order of the deque, returning an order code that
2457-
/// indicates whether its argument is `Less`, `Equal` or `Greater`
2458-
/// than the desired target.
2454+
/// The comparator function should return an order code that indicates
2455+
/// whether its argument is `Less`, `Equal` or `Greater` the desired
2456+
/// target.
2457+
/// If the `VecDeque` is not sorted or if the comparator function does not
2458+
/// implement an order consistent with the sort order of the underlying
2459+
/// `VecDeque`, the returned result is unspecified and meaningless.
24592460
///
24602461
/// If the value is found then [`Result::Ok`] is returned, containing the
24612462
/// index of the matching element. If there are multiple matches, then any
@@ -2465,7 +2466,6 @@ impl<T, A: Allocator> VecDeque<T, A> {
24652466
///
24662467
/// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
24672468
///
2468-
/// [`contains`]: VecDeque::contains
24692469
/// [`binary_search`]: VecDeque::binary_search
24702470
/// [`binary_search_by_key`]: VecDeque::binary_search_by_key
24712471
/// [`partition_point`]: VecDeque::partition_point
@@ -2505,10 +2505,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
25052505
}
25062506

25072507
/// Binary searches this `VecDeque` with a key extraction function.
2508-
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
25092508
///
25102509
/// Assumes that the deque is sorted by the key, for instance with
25112510
/// [`make_contiguous().sort_by_key()`] using the same key extraction function.
2511+
/// If the deque is not sorted by the key, the returned result is
2512+
/// unspecified and meaningless.
25122513
///
25132514
/// If the value is found then [`Result::Ok`] is returned, containing the
25142515
/// index of the matching element. If there are multiple matches, then any
@@ -2518,7 +2519,6 @@ impl<T, A: Allocator> VecDeque<T, A> {
25182519
///
25192520
/// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
25202521
///
2521-
/// [`contains`]: VecDeque::contains
25222522
/// [`make_contiguous().sort_by_key()`]: VecDeque::make_contiguous
25232523
/// [`binary_search`]: VecDeque::binary_search
25242524
/// [`binary_search_by`]: VecDeque::binary_search_by

library/core/src/slice/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,8 @@ impl<T> [T] {
23872387
}
23882388

23892389
/// Binary searches this slice for a given element.
2390-
/// This behaves similarly to [`contains`] if this slice is sorted.
2390+
/// If the slice is not sorted, the returned result is unspecified and
2391+
/// meaningless.
23912392
///
23922393
/// If the value is found then [`Result::Ok`] is returned, containing the
23932394
/// index of the matching element. If there are multiple matches, then any
@@ -2399,7 +2400,6 @@ impl<T> [T] {
23992400
///
24002401
/// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
24012402
///
2402-
/// [`contains`]: slice::contains
24032403
/// [`binary_search_by`]: slice::binary_search_by
24042404
/// [`binary_search_by_key`]: slice::binary_search_by_key
24052405
/// [`partition_point`]: slice::partition_point
@@ -2462,12 +2462,13 @@ impl<T> [T] {
24622462
}
24632463

24642464
/// Binary searches this slice with a comparator function.
2465-
/// This behaves similarly to [`contains`] if this slice is sorted.
24662465
///
2467-
/// The comparator function should implement an order consistent
2468-
/// with the sort order of the underlying slice, returning an
2469-
/// order code that indicates whether its argument is `Less`,
2470-
/// `Equal` or `Greater` the desired target.
2466+
/// The comparator function should return an order code that indicates
2467+
/// whether its argument is `Less`, `Equal` or `Greater` the desired
2468+
/// target.
2469+
/// If the slice is not sorted or if the comparator function does not
2470+
/// implement an order consistent with the sort order of the underlying
2471+
/// slice, the returned result is unspecified and meaningless.
24712472
///
24722473
/// If the value is found then [`Result::Ok`] is returned, containing the
24732474
/// index of the matching element. If there are multiple matches, then any
@@ -2479,7 +2480,6 @@ impl<T> [T] {
24792480
///
24802481
/// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
24812482
///
2482-
/// [`contains`]: slice::contains
24832483
/// [`binary_search`]: slice::binary_search
24842484
/// [`binary_search_by_key`]: slice::binary_search_by_key
24852485
/// [`partition_point`]: slice::partition_point
@@ -2548,10 +2548,11 @@ impl<T> [T] {
25482548
}
25492549

25502550
/// Binary searches this slice with a key extraction function.
2551-
/// This behaves similarly to [`contains`] if this slice is sorted.
25522551
///
25532552
/// Assumes that the slice is sorted by the key, for instance with
25542553
/// [`sort_by_key`] using the same key extraction function.
2554+
/// If the slice is not sorted by the key, the returned result is
2555+
/// unspecified and meaningless.
25552556
///
25562557
/// If the value is found then [`Result::Ok`] is returned, containing the
25572558
/// index of the matching element. If there are multiple matches, then any
@@ -2563,7 +2564,6 @@ impl<T> [T] {
25632564
///
25642565
/// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
25652566
///
2566-
/// [`contains`]: slice::contains
25672567
/// [`sort_by_key`]: slice::sort_by_key
25682568
/// [`binary_search`]: slice::binary_search
25692569
/// [`binary_search_by`]: slice::binary_search_by

0 commit comments

Comments
 (0)