Skip to content

Commit

Permalink
Improve the documentation of drain members
Browse files Browse the repository at this point in the history
  • Loading branch information
ssomers committed Feb 8, 2022
1 parent 2a8dbdb commit f1b48b9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ impl<T: Ord> BinaryHeap<T> {

/// Returns an iterator which retrieves elements in heap order.
/// The retrieved elements are removed from the original heap.
/// The remaining elements will be removed on drop in heap order.
/// On drop, the remaining elements are removed and dropped in heap order.
///
/// Note:
/// * `.drain_sorted()` is *O*(*n* \* log(*n*)); much slower than `.drain()`.
Expand Down
17 changes: 8 additions & 9 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,15 +1215,14 @@ impl<T, A: Allocator> VecDeque<T, A> {
unsafe { IterMut::new(ring, tail, head, PhantomData) }
}

/// Creates a draining iterator that removes the specified range in the
/// `VecDeque` and yields the removed items.
/// Removes the specified range from the `VecDeque`, returning all removed
/// elements as an iterator.
///
/// Note 1: The element range is removed even if the iterator is not
/// consumed until the end.
///
/// Note 2: It is unspecified how many elements are removed from the deque,
/// if the `Drain` value is not dropped, but the borrow it holds expires
/// (e.g., due to `mem::forget`).
/// When the iterator **is** dropped, it drops any elements that it has not
/// yet yielded (none if the iterator was fully consumed).
/// If the iterator **is not** dropped (with [`mem::forget`], for example),
/// it is unspecified which elements remain in the vector; even elements
/// outside the range may have been removed and leaked.
///
/// # Panics
///
Expand All @@ -1240,7 +1239,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// assert_eq!(drained, [3]);
/// assert_eq!(v, [1, 2]);
///
/// // A full range clears all contents
/// // A full range clears all contents, like `clear()` does
/// v.drain(..);
/// assert!(v.is_empty());
/// ```
Expand Down
9 changes: 3 additions & 6 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,11 +1628,8 @@ impl String {
self.vec.clear()
}

/// Creates a draining iterator that removes the specified range in the `String`
/// and yields the removed `chars`.
///
/// Note: The element range is removed even if the iterator is not
/// consumed until the end.
/// Removes the specified range from the string, returning all removed
/// characters as an iterator.
///
/// # Panics
///
Expand All @@ -1652,7 +1649,7 @@ impl String {
/// assert_eq!(t, "α is alpha, ");
/// assert_eq!(s, "β is beta");
///
/// // A full range clears the string
/// // A full range clears the string, like `clear()` does
/// s.drain(..);
/// assert_eq!(s, "");
/// ```
Expand Down
15 changes: 8 additions & 7 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,13 +1799,14 @@ impl<T, A: Allocator> Vec<T, A> {
self.len += count;
}

/// Creates a draining iterator that removes the specified range in the vector
/// and yields the removed items.
/// Removes the specified range from the vector, returning all removed
/// elements as an iterator.
///
/// When the iterator **is** dropped, all elements in the range are removed
/// from the vector, even if the iterator was not fully consumed. If the
/// iterator **is not** dropped (with [`mem::forget`] for example), it is
/// unspecified how many elements are removed.
/// When the iterator **is** dropped, it drops any elements that it has not
/// yet yielded (none if the iterator was fully consumed).
/// If the iterator **is not** dropped (with [`mem::forget`], for example),
/// it is unspecified which elements remain in the vector; even elements
/// outside the range may have been removed and leaked.
///
/// # Panics
///
Expand All @@ -1820,7 +1821,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert_eq!(v, &[1]);
/// assert_eq!(u, &[2, 3]);
///
/// // A full range clears the vector
/// // A full range clears the vector, like `clear()` does
/// v.drain(..);
/// assert_eq!(v, &[]);
/// ```
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<T, S> HashSet<T, S> {
self.base.is_empty()
}

/// Clears the set, returning all elements in an iterator.
/// Clears the set, returning all elements as an iterator.
///
/// # Examples
///
Expand Down

0 comments on commit f1b48b9

Please sign in to comment.