Skip to content

Commit c186460

Browse files
committed
Fix some confusing wording and improve slice-search-related docs
1 parent e08d569 commit c186460

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

library/alloc/src/collections/linked_list.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl<T> LinkedList<T> {
645645
/// Returns `true` if the `LinkedList` contains an element equal to the
646646
/// given value.
647647
///
648-
/// This operation should compute in *O*(*n*) time.
648+
/// This operation should compute linearly in *O*(*n*) time.
649649
///
650650
/// # Examples
651651
///
@@ -1569,7 +1569,7 @@ impl<'a, T> CursorMut<'a, T> {
15691569
/// Appends an element to the front of the cursor's parent list. The node
15701570
/// that the cursor points to is unchanged, even if it is the "ghost" node.
15711571
///
1572-
/// This operation should compute in O(1) time.
1572+
/// This operation should compute in *O*(1) time.
15731573
// `push_front` continues to point to "ghost" when it addes a node to mimic
15741574
// the behavior of `insert_before` on an empty list.
15751575
#[unstable(feature = "linked_list_cursors", issue = "58533")]
@@ -1584,7 +1584,7 @@ impl<'a, T> CursorMut<'a, T> {
15841584
/// Appends an element to the back of the cursor's parent list. The node
15851585
/// that the cursor points to is unchanged, even if it is the "ghost" node.
15861586
///
1587-
/// This operation should compute in O(1) time.
1587+
/// This operation should compute in *O*(1) time.
15881588
#[unstable(feature = "linked_list_cursors", issue = "58533")]
15891589
pub fn push_back(&mut self, elt: T) {
15901590
// Safety: We know that `push_back` does not change the position in
@@ -1603,7 +1603,7 @@ impl<'a, T> CursorMut<'a, T> {
16031603
/// unchanged, unless it was pointing to the front element. In that case, it
16041604
/// points to the new front element.
16051605
///
1606-
/// This operation should compute in O(1) time.
1606+
/// This operation should compute in *O*(1) time.
16071607
#[unstable(feature = "linked_list_cursors", issue = "58533")]
16081608
pub fn pop_front(&mut self) -> Option<T> {
16091609
// We can't check if current is empty, we must check the list directly.
@@ -1630,7 +1630,7 @@ impl<'a, T> CursorMut<'a, T> {
16301630
/// unchanged, unless it was pointing to the back element. In that case, it
16311631
/// points to the "ghost" element.
16321632
///
1633-
/// This operation should compute in O(1) time.
1633+
/// This operation should compute in *O*(1) time.
16341634
#[unstable(feature = "linked_list_cursors", issue = "58533")]
16351635
pub fn pop_back(&mut self) -> Option<T> {
16361636
if self.list.is_empty() {

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
13221322
/// Returns `true` if the deque contains an element equal to the
13231323
/// given value.
13241324
///
1325+
/// This operation is *O*(*n*).
1326+
///
1327+
/// Note that if you have a sorted `VecDeque`, [`binary_search`] may be faster.
1328+
///
1329+
/// [`binary_search`]: VecDeque::binary_search
1330+
///
13251331
/// # Examples
13261332
///
13271333
/// ```
@@ -2528,7 +2534,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
25282534
}
25292535
}
25302536

2531-
/// Binary searches the sorted deque for a given element.
2537+
/// Binary searches this `VecDeque` for a given element.
2538+
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
25322539
///
25332540
/// If the value is found then [`Result::Ok`] is returned, containing the
25342541
/// index of the matching element. If there are multiple matches, then any
@@ -2538,6 +2545,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
25382545
///
25392546
/// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
25402547
///
2548+
/// [`contains`]: VecDeque::contains
25412549
/// [`binary_search_by`]: VecDeque::binary_search_by
25422550
/// [`binary_search_by_key`]: VecDeque::binary_search_by_key
25432551
/// [`partition_point`]: VecDeque::partition_point
@@ -2581,7 +2589,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
25812589
self.binary_search_by(|e| e.cmp(x))
25822590
}
25832591

2584-
/// Binary searches the sorted deque with a comparator function.
2592+
/// Binary searches this `VecDeque` with a comparator function.
2593+
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
25852594
///
25862595
/// The comparator function should implement an order consistent
25872596
/// with the sort order of the deque, returning an order code that
@@ -2596,6 +2605,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
25962605
///
25972606
/// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
25982607
///
2608+
/// [`contains`]: VecDeque::contains
25992609
/// [`binary_search`]: VecDeque::binary_search
26002610
/// [`binary_search_by_key`]: VecDeque::binary_search_by_key
26012611
/// [`partition_point`]: VecDeque::partition_point
@@ -2634,7 +2644,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
26342644
}
26352645
}
26362646

2637-
/// Binary searches the sorted deque with a key extraction function.
2647+
/// Binary searches this `VecDeque` with a key extraction function.
2648+
/// This behaves similarly to [`contains`] if this `VecDeque` is sorted.
26382649
///
26392650
/// Assumes that the deque is sorted by the key, for instance with
26402651
/// [`make_contiguous().sort_by_key()`] using the same key extraction function.
@@ -2647,6 +2658,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
26472658
///
26482659
/// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
26492660
///
2661+
/// [`contains`]: VecDeque::contains
26502662
/// [`make_contiguous().sort_by_key()`]: VecDeque::make_contiguous
26512663
/// [`binary_search`]: VecDeque::binary_search
26522664
/// [`binary_search_by`]: VecDeque::binary_search_by

library/core/src/slice/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,12 @@ impl<T> [T] {
20952095

20962096
/// Returns `true` if the slice contains an element with the given value.
20972097
///
2098+
/// This operation is *O*(*n*).
2099+
///
2100+
/// Note that if you have a sorted slice, [`binary_search`] may be faster.
2101+
///
2102+
/// [`binary_search`]: slice::binary_search
2103+
///
20982104
/// # Examples
20992105
///
21002106
/// ```
@@ -2251,7 +2257,8 @@ impl<T> [T] {
22512257
None
22522258
}
22532259

2254-
/// Binary searches this sorted slice for a given element.
2260+
/// Binary searches this slice for a given element.
2261+
/// This behaves similary to [`contains`] if this slice is sorted.
22552262
///
22562263
/// If the value is found then [`Result::Ok`] is returned, containing the
22572264
/// index of the matching element. If there are multiple matches, then any
@@ -2263,6 +2270,7 @@ impl<T> [T] {
22632270
///
22642271
/// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
22652272
///
2273+
/// [`contains`]: slice::contains
22662274
/// [`binary_search_by`]: slice::binary_search_by
22672275
/// [`binary_search_by_key`]: slice::binary_search_by_key
22682276
/// [`partition_point`]: slice::partition_point
@@ -2301,7 +2309,8 @@ impl<T> [T] {
23012309
self.binary_search_by(|p| p.cmp(x))
23022310
}
23032311

2304-
/// Binary searches this sorted slice with a comparator function.
2312+
/// Binary searches this slice with a comparator function.
2313+
/// This behaves similarly to [`contains`] if this slice is sorted.
23052314
///
23062315
/// The comparator function should implement an order consistent
23072316
/// with the sort order of the underlying slice, returning an
@@ -2318,6 +2327,7 @@ impl<T> [T] {
23182327
///
23192328
/// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
23202329
///
2330+
/// [`contains`]: slice::contains
23212331
/// [`binary_search`]: slice::binary_search
23222332
/// [`binary_search_by_key`]: slice::binary_search_by_key
23232333
/// [`partition_point`]: slice::partition_point
@@ -2376,7 +2386,8 @@ impl<T> [T] {
23762386
Err(left)
23772387
}
23782388

2379-
/// Binary searches this sorted slice with a key extraction function.
2389+
/// Binary searches this slice with a key extraction function.
2390+
/// This behaves similarly to [`contains`] if this slice is sorted.
23802391
///
23812392
/// Assumes that the slice is sorted by the key, for instance with
23822393
/// [`sort_by_key`] using the same key extraction function.
@@ -2391,6 +2402,7 @@ impl<T> [T] {
23912402
///
23922403
/// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
23932404
///
2405+
/// [`contains`]: slice::contains
23942406
/// [`sort_by_key`]: slice::sort_by_key
23952407
/// [`binary_search`]: slice::binary_search
23962408
/// [`binary_search_by`]: slice::binary_search_by

0 commit comments

Comments
 (0)