Skip to content

Commit f963eb2

Browse files
committed
Auto merge of #30272 - tshepang:doc-drain, r=bluss
Second sentence actually repeats info from first sentence. "from start to end" also feels like it adds nothing. I also extended Vec::drain example.
2 parents 27d5511 + 46e2296 commit f963eb2

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/libcollections/string.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,10 @@ impl String {
12011201
}
12021202

12031203
/// Create a draining iterator that removes the specified range in the string
1204-
/// and yields the removed chars from start to end. The element range is
1205-
/// removed even if the iterator is not consumed until the end.
1204+
/// and yields the removed chars.
1205+
///
1206+
/// Note: The element range is removed even if the iterator is not
1207+
/// consumed until the end.
12061208
///
12071209
/// # Panics
12081210
///

src/libcollections/vec.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,12 @@ impl<T> Vec<T> {
725725
}
726726

727727
/// Create a draining iterator that removes the specified range in the vector
728-
/// and yields the removed items from start to end. The element range is
729-
/// removed even if the iterator is not consumed until the end.
728+
/// and yields the removed items.
730729
///
731-
/// Note: It is unspecified how many elements are removed from the vector,
730+
/// Note 1: The element range is removed even if the iterator is not
731+
/// consumed until the end.
732+
///
733+
/// Note 2: It is unspecified how many elements are removed from the vector,
732734
/// if the `Drain` value is leaked.
733735
///
734736
/// # Panics
@@ -739,11 +741,14 @@ impl<T> Vec<T> {
739741
/// # Examples
740742
///
741743
/// ```
742-
/// // Draining using `..` clears the whole vector.
743744
/// let mut v = vec![1, 2, 3];
744-
/// let u: Vec<_> = v.drain(..).collect();
745+
/// let u: Vec<_> = v.drain(1..).collect();
746+
/// assert_eq!(v, &[1]);
747+
/// assert_eq!(u, &[2, 3]);
748+
///
749+
/// // A full range clears the vector
750+
/// v.drain(..);
745751
/// assert_eq!(v, &[]);
746-
/// assert_eq!(u, &[1, 2, 3]);
747752
/// ```
748753
#[stable(feature = "drain", since = "1.6.0")]
749754
pub fn drain<R>(&mut self, range: R) -> Drain<T>

src/libcollections/vec_deque.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,12 @@ impl<T> VecDeque<T> {
763763
}
764764

765765
/// Create a draining iterator that removes the specified range in the
766-
/// `VecDeque` and yields the removed items from start to end. The element
767-
/// range is removed even if the iterator is not consumed until the end.
766+
/// `VecDeque` and yields the removed items.
768767
///
769-
/// Note: It is unspecified how many elements are removed from the deque,
768+
/// Note 1: The element range is removed even if the iterator is not
769+
/// consumed until the end.
770+
///
771+
/// Note 2: It is unspecified how many elements are removed from the deque,
770772
/// if the `Drain` value is not dropped, but the borrow it holds expires
771773
/// (eg. due to mem::forget).
772774
///
@@ -779,11 +781,13 @@ impl<T> VecDeque<T> {
779781
///
780782
/// ```
781783
/// use std::collections::VecDeque;
784+
785+
/// let mut v: VecDeque<_> = vec![1, 2, 3].into_iter().collect();
786+
/// assert_eq!(vec![3].into_iter().collect::<VecDeque<_>>(), v.drain(2..).collect());
787+
/// assert_eq!(vec![1, 2].into_iter().collect::<VecDeque<_>>(), v);
782788
///
783-
/// // draining using `..` clears the whole deque.
784-
/// let mut v = VecDeque::new();
785-
/// v.push_back(1);
786-
/// assert_eq!(v.drain(..).next(), Some(1));
789+
/// // A full range clears all contents
790+
/// v.drain(..);
787791
/// assert!(v.is_empty());
788792
/// ```
789793
#[inline]

0 commit comments

Comments
 (0)