about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTy Coghlan <Coghlan.ty@gmail.com>2016-05-26 18:17:30 -0400
committerTy Coghlan <Coghlan.ty@gmail.com>2016-05-27 17:53:13 -0400
commitfeb0b27e41b4048ce30f23231b787e7c2d2e27d3 (patch)
treeaa53d16352ecf70a9e203375da9c3ead28bba8df /src
parent97e3a2401e4b2f659d69ed0c0822cae04e3495b7 (diff)
downloadrust-feb0b27e41b4048ce30f23231b787e7c2d2e27d3.tar.gz
rust-feb0b27e41b4048ce30f23231b787e7c2d2e27d3.zip
Added examples/docs to split in str.rs
Added documentation clarifying the behavior of split when used with the empty string and contiguous separators.
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/str.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs
index 2059943bfdf..f3770816cb6 100644
--- a/src/libcollections/str.rs
+++ b/src/libcollections/str.rs
@@ -1097,8 +1097,34 @@ impl str {
     /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
     /// ```
     ///
-    /// This can lead to possibly surprising behavior when whitespace is used
-    /// as the separator. This code is correct:
+    /// Contiguous separators are separated by the empty string.
+    ///
+    /// ```
+    /// let x = "(///)".to_string();
+    /// let d: Vec<_> = x.split('/').collect();;
+    ///
+    /// assert_eq!(d, &["(", "", "", ")"]);
+    /// ```
+    ///
+    /// Separators at the start or end of a string are neighbored
+    /// by empty strings.
+    ///
+    /// ```
+    /// let d: Vec<_> = "010".split("0").collect();
+    /// assert_eq!(d, &["", "1", ""]);
+    /// ```
+    ///
+    /// When the empty string is used as a separator, it separates
+    /// every character in the string, along with the beginning
+    /// and end of the string.
+    ///
+    /// ```
+    /// let f: Vec<_> = "rust".split("").collect();
+    /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
+    /// ```
+    ///
+    /// Contiguous separators can lead to possibly surprising behavior
+    /// when whitespace is used as the separator. This code is correct:
     ///
     /// ```
     /// let x = "    a  b c".to_string();