From feb0b27e41b4048ce30f23231b787e7c2d2e27d3 Mon Sep 17 00:00:00 2001 From: Ty Coghlan Date: Thu, 26 May 2016 18:17:30 -0400 Subject: [PATCH] Added examples/docs to split in str.rs Added documentation clarifying the behavior of split when used with the empty string and contiguous separators. --- src/libcollections/str.rs | 30 ++++++++++++++++++++++++++++-- 1 file 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();