Skip to content

Commit 8c4e0d4

Browse files
committed
add examples to vec::Drain{,Filter}::keep_rest docs
1 parent 50c98a8 commit 8c4e0d4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

library/alloc/src/vec/drain.rs

+18
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
6767
}
6868

6969
/// Keep unyielded elements in the source `Vec`.
70+
///
71+
/// # Examples
72+
///
73+
/// ```
74+
/// #![feature(drain_keep_rest)]
75+
///
76+
/// let mut vec = vec!['a', 'b', 'c'];
77+
/// let mut drain = vec.drain(..);
78+
///
79+
/// assert_eq!(drain.next().unwrap(), 'a');
80+
///
81+
/// // This call keeps 'b' and 'c' in the vec.
82+
/// drain.keep_rest();
83+
///
84+
/// // If we wouldn't call `keep_rest()`,
85+
/// // `vec` would be empty.
86+
/// assert_eq!(vec, ['b', 'c']);
87+
/// ```
7088
#[unstable(feature = "drain_keep_rest", issue = "none")]
7189
pub fn keep_rest(self) {
7290
// At this moment layout looks like this:

library/alloc/src/vec/drain_filter.rs

+19
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ where
5757
}
5858

5959
/// Keep unyielded elements in the source `Vec`.
60+
///
61+
/// # Examples
62+
///
63+
/// ```
64+
/// #![feature(drain_filter)]
65+
/// #![feature(drain_keep_rest)]
66+
///
67+
/// let mut vec = vec!['a', 'b', 'c'];
68+
/// let mut drain = vec.drain_filter(|_| true);
69+
///
70+
/// assert_eq!(drain.next().unwrap(), 'a');
71+
///
72+
/// // This call keeps 'b' and 'c' in the vec.
73+
/// drain.keep_rest();
74+
///
75+
/// // If we wouldn't call `keep_rest()`,
76+
/// // `vec` would be empty.
77+
/// assert_eq!(vec, ['b', 'c']);
78+
/// ```
6079
#[unstable(feature = "drain_keep_rest", issue = "none")]
6180
pub fn keep_rest(self) {
6281
// At this moment layout looks like this:

0 commit comments

Comments
 (0)