Skip to content

Commit feb0b27

Browse files
committed
Added examples/docs to split in str.rs
Added documentation clarifying the behavior of split when used with the empty string and contiguous separators.
1 parent 97e3a24 commit feb0b27

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/libcollections/str.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,34 @@ impl str {
10971097
/// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
10981098
/// ```
10991099
///
1100-
/// This can lead to possibly surprising behavior when whitespace is used
1101-
/// as the separator. This code is correct:
1100+
/// Contiguous separators are separated by the empty string.
1101+
///
1102+
/// ```
1103+
/// let x = "(///)".to_string();
1104+
/// let d: Vec<_> = x.split('/').collect();;
1105+
///
1106+
/// assert_eq!(d, &["(", "", "", ")"]);
1107+
/// ```
1108+
///
1109+
/// Separators at the start or end of a string are neighbored
1110+
/// by empty strings.
1111+
///
1112+
/// ```
1113+
/// let d: Vec<_> = "010".split("0").collect();
1114+
/// assert_eq!(d, &["", "1", ""]);
1115+
/// ```
1116+
///
1117+
/// When the empty string is used as a separator, it separates
1118+
/// every character in the string, along with the beginning
1119+
/// and end of the string.
1120+
///
1121+
/// ```
1122+
/// let f: Vec<_> = "rust".split("").collect();
1123+
/// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1124+
/// ```
1125+
///
1126+
/// Contiguous separators can lead to possibly surprising behavior
1127+
/// when whitespace is used as the separator. This code is correct:
11021128
///
11031129
/// ```
11041130
/// let x = " a b c".to_string();

0 commit comments

Comments
 (0)