]> git.lizzy.rs Git - rust.git/commitdiff
add examples to `vec::Drain{,Filter}::keep_rest` docs
authorMaybe Waffle <waffle.lapkin@gmail.com>
Sun, 28 Aug 2022 12:38:26 +0000 (16:38 +0400)
committerMaybe Waffle <waffle.lapkin@gmail.com>
Sun, 28 Aug 2022 12:58:06 +0000 (16:58 +0400)
library/alloc/src/vec/drain.rs
library/alloc/src/vec/drain_filter.rs

index 3e350c2b38e3e6448658000324a6e9f917e28db8..2a3f7ff6d2a1c9caa9559ada2530ae9b0de78865 100644 (file)
@@ -67,6 +67,24 @@ pub fn allocator(&self) -> &A {
     }
 
     /// Keep unyielded elements in the source `Vec`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(drain_keep_rest)]
+    ///
+    /// let mut vec = vec!['a', 'b', 'c'];
+    /// let mut drain = vec.drain(..);
+    ///
+    /// assert_eq!(drain.next().unwrap(), 'a');
+    ///
+    /// // This call keeps 'b' and 'c' in the vec.
+    /// drain.keep_rest();
+    ///
+    /// // If we wouldn't call `keep_rest()`,
+    /// // `vec` would be empty.
+    /// assert_eq!(vec, ['b', 'c']);
+    /// ```
     #[unstable(feature = "drain_keep_rest", issue = "none")]
     pub fn keep_rest(self) {
         // At this moment layout looks like this:
index a5c1eab94eef98fb1788cac12f4ec963bf5b0926..3130c1f1cca3112de4b873befbbe0041d5abb561 100644 (file)
@@ -57,6 +57,25 @@ pub fn allocator(&self) -> &A {
     }
 
     /// Keep unyielded elements in the source `Vec`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(drain_filter)]
+    /// #![feature(drain_keep_rest)]
+    ///
+    /// let mut vec = vec!['a', 'b', 'c'];
+    /// let mut drain = vec.drain_filter(|_| true);
+    ///
+    /// assert_eq!(drain.next().unwrap(), 'a');
+    ///
+    /// // This call keeps 'b' and 'c' in the vec.
+    /// drain.keep_rest();
+    ///
+    /// // If we wouldn't call `keep_rest()`,
+    /// // `vec` would be empty.
+    /// assert_eq!(vec, ['b', 'c']);
+    /// ```
     #[unstable(feature = "drain_keep_rest", issue = "none")]
     pub fn keep_rest(self) {
         // At this moment layout looks like this: