@@ -1180,9 +1180,8 @@ impl str {
1180
1180
/// matched by a pattern.
1181
1181
///
1182
1182
/// The pattern can be a simple `&str`, `char`, or a closure that
1183
- /// determines the split.
1184
- /// Additional libraries might provide more complex patterns like
1185
- /// regular expressions.
1183
+ /// determines the split. Additional libraries might provide more complex
1184
+ /// patterns like regular expressions.
1186
1185
///
1187
1186
/// # Iterator behavior
1188
1187
///
@@ -1224,6 +1223,32 @@ impl str {
1224
1223
/// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1225
1224
/// assert_eq!(v, ["abc", "def", "ghi"]);
1226
1225
/// ```
1226
+ ///
1227
+ /// If a string contains multiple contiguous separators, you will end up
1228
+ /// with empty strings in the output:
1229
+ ///
1230
+ /// ```
1231
+ /// let x = "||||a||b|c".to_string();
1232
+ /// let d: Vec<_> = x.split('|').collect();
1233
+ ///
1234
+ /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1235
+ /// ```
1236
+ ///
1237
+ /// This can lead to possibly surprising behavior when whitespace is used
1238
+ /// as the separator. This code is correct:
1239
+ ///
1240
+ /// ```
1241
+ /// let x = " a b c".to_string();
1242
+ /// let d: Vec<_> = x.split(' ').collect();
1243
+ ///
1244
+ /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1245
+ /// ```
1246
+ ///
1247
+ /// It does _not_ give you:
1248
+ ///
1249
+ /// ```rust,ignore
1250
+ /// assert_eq!(d, &["a", "b", "c"]);
1251
+ /// ```
1227
1252
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1228
1253
pub fn split < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Split < ' a , P > {
1229
1254
core_str:: StrExt :: split ( & self [ ..] , pat)
0 commit comments