about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-06-10 05:28:03 +0000
committerbors <bors@rust-lang.org>2015-06-10 05:28:03 +0000
commite954031ea2d17ff0ee3f197830cd82be80cb55be (patch)
treef260f9192e0c9b60ff924acd33a94371400e730a
parent172cd83490cc66065e72861aed53e3efec29b34f (diff)
parent96c7a6b8fe283ad4f3e58bfc7b1c9ae6fa7cce00 (diff)
downloadrust-e954031ea2d17ff0ee3f197830cd82be80cb55be.tar.gz
rust-e954031ea2d17ff0ee3f197830cd82be80cb55be.zip
Auto merge of #26130 - steveklabnik:gh25986, r=alexcrichton
This can be confusing when whitespace is the separator

Fixes #25986
-rw-r--r--src/libcollections/str.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs
index ef8039c565a..ba378a056c7 100644
--- a/src/libcollections/str.rs
+++ b/src/libcollections/str.rs
@@ -1180,9 +1180,8 @@ impl str {
     /// matched by a pattern.
     ///
     /// The pattern can be a simple `&str`, `char`, or a closure that
-    /// determines the split.
-    /// Additional libraries might provide more complex patterns like
-    /// regular expressions.
+    /// determines the split. Additional libraries might provide more complex
+    /// patterns like regular expressions.
     ///
     /// # Iterator behavior
     ///
@@ -1224,6 +1223,32 @@ impl str {
     /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
     /// assert_eq!(v, ["abc", "def", "ghi"]);
     /// ```
+    ///
+    /// If a string contains multiple contiguous separators, you will end up
+    /// with empty strings in the output:
+    ///
+    /// ```
+    /// let x = "||||a||b|c".to_string();
+    /// let d: Vec<_> = x.split('|').collect();
+    ///
+    /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
+    /// ```
+    ///
+    /// This can lead to possibly surprising behavior when whitespace is used
+    /// as the separator. This code is correct:
+    ///
+    /// ```
+    /// let x = "    a  b c".to_string();
+    /// let d: Vec<_> = x.split(' ').collect();
+    ///
+    /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
+    /// ```
+    ///
+    /// It does _not_ give you:
+    ///
+    /// ```rust,ignore
+    /// assert_eq!(d, &["a", "b", "c"]);
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
         core_str::StrExt::split(&self[..], pat)