about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2016-07-26 17:21:13 -0400
committerGitHub <noreply@github.com>2016-07-26 17:21:13 -0400
commit1a3c46fdec3f3cdc79b5d45439dd87500faeb787 (patch)
tree90a814d5746f44858c7d937bd0ba785a30cb93e7 /src
parent8de36f14233421fa3988b4b649dbdee75c76e3ec (diff)
parenta139772e7751017d66d82877b3666d58df7ef00b (diff)
downloadrust-1a3c46fdec3f3cdc79b5d45439dd87500faeb787.tar.gz
rust-1a3c46fdec3f3cdc79b5d45439dd87500faeb787.zip
Rollup merge of #35019 - frewsxcv:slice-split, r=GuillaumeGomez
Rewrite/expansion of `slice::split` doc examples.

None
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/slice.rs37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs
index 1f8eea56fc6..ccef6c02f9d 100644
--- a/src/libcollections/slice.rs
+++ b/src/libcollections/slice.rs
@@ -691,15 +691,40 @@ impl<T> [T] {
     ///
     /// # Examples
     ///
-    /// Print the slice split by numbers divisible by 3 (i.e. `[10, 40]`,
-    /// `[20]`, `[50]`):
+    /// ```
+    /// let slice = [10, 40, 33, 20];
+    /// let mut iter = slice.split(|num| num % 3 == 0);
     ///
+    /// assert_eq!(iter.next().unwrap(), &[10, 40]);
+    /// assert_eq!(iter.next().unwrap(), &[20]);
+    /// assert!(iter.next().is_none());
     /// ```
-    /// let v = [10, 40, 30, 20, 60, 50];
     ///
-    /// for group in v.split(|num| *num % 3 == 0) {
-    ///     println!("{:?}", group);
-    /// }
+    /// If the first element is matched, an empty slice will be the first item
+    /// returned by the iterator. Similarly, if the last element in the slice
+    /// is matched, an empty slice will be the last item returned by the
+    /// iterator:
+    ///
+    /// ```
+    /// let slice = [10, 40, 33];
+    /// let mut iter = slice.split(|num| num % 3 == 0);
+    ///
+    /// assert_eq!(iter.next().unwrap(), &[10, 40]);
+    /// assert_eq!(iter.next().unwrap(), &[]);
+    /// assert!(iter.next().is_none());
+    /// ```
+    ///
+    /// If two matched elements are directly adjacent, an empty slice will be
+    /// present between them:
+    ///
+    /// ```
+    /// let slice = [10, 6, 33, 20];
+    /// let mut iter = slice.split(|num| num % 3 == 0);
+    ///
+    /// assert_eq!(iter.next().unwrap(), &[10]);
+    /// assert_eq!(iter.next().unwrap(), &[]);
+    /// assert_eq!(iter.next().unwrap(), &[20]);
+    /// assert!(iter.next().is_none());
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]