diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-06-09 14:42:55 -0400 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-06-09 14:42:55 -0400 |
| commit | 96c7a6b8fe283ad4f3e58bfc7b1c9ae6fa7cce00 (patch) | |
| tree | 74afc20f8dffbbdded8938de1df7d035e4cdc70f | |
| parent | 7b0f2af27f18b6c81fe6a2faab0ba96e0da3bba5 (diff) | |
| download | rust-96c7a6b8fe283ad4f3e58bfc7b1c9ae6fa7cce00.tar.gz rust-96c7a6b8fe283ad4f3e58bfc7b1c9ae6fa7cce00.zip | |
Document str::split behavior with contiguous separators
This can be confusing when whitespace is the separator Fixes #25986
| -rw-r--r-- | src/libcollections/str.rs | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index a9725214c19..a581f939cdf 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) |
