Skip to content

Commit 8f9f2fe

Browse files
committed
Auto merge of #25912 - tshepang:better-str-examples, r=bluss
2 parents 776f87e + 0335a94 commit 8f9f2fe

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

src/libcollections/str.rs

+28-50
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,20 @@ impl str {
640640
///
641641
/// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
642642
/// assert_eq!(v, ["lion", "tiger", "leopard"]);
643-
/// ```
644-
///
645-
/// More complex patterns with closures:
646643
///
647-
/// ```
648-
/// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect();
644+
/// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
649645
/// assert_eq!(v, ["abc", "def", "ghi"]);
650646
///
651647
/// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
652648
/// assert_eq!(v, ["lion", "tiger", "leopard"]);
653649
/// ```
650+
///
651+
/// A more complex pattern, using a closure:
652+
///
653+
/// ```
654+
/// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
655+
/// assert_eq!(v, ["abc", "def", "ghi"]);
656+
/// ```
654657
#[stable(feature = "rust1", since = "1.0.0")]
655658
pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
656659
core_str::StrExt::split(&self[..], pat)
@@ -691,14 +694,11 @@ impl str {
691694
/// assert_eq!(v, ["leopard", "tiger", "lion"]);
692695
/// ```
693696
///
694-
/// More complex patterns with closures:
697+
/// A more complex pattern, using a closure:
695698
///
696-
/// ```rust
697-
/// let v: Vec<&str> = "abc1def2ghi".rsplit(|c: char| c.is_numeric()).collect();
699+
/// ```
700+
/// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
698701
/// assert_eq!(v, ["ghi", "def", "abc"]);
699-
///
700-
/// let v: Vec<&str> = "lionXtigerXleopard".rsplit(char::is_uppercase).collect();
701-
/// assert_eq!(v, ["leopard", "tiger", "lion"]);
702702
/// ```
703703
#[stable(feature = "rust1", since = "1.0.0")]
704704
pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
@@ -733,22 +733,13 @@ impl str {
733733
///
734734
/// # Examples
735735
///
736-
/// Simple patterns:
737-
///
738736
/// ```
739737
/// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
740738
/// assert_eq!(v, ["A", "B"]);
741739
///
742740
/// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
743741
/// assert_eq!(v, ["A", "", "B", ""]);
744742
/// ```
745-
///
746-
/// More complex patterns with closures:
747-
///
748-
/// ```
749-
/// let v: Vec<&str> = "abc1def2ghi3".split_terminator(|c: char| c.is_numeric()).collect();
750-
/// assert_eq!(v, ["abc", "def", "ghi"]);
751-
/// ```
752743
#[stable(feature = "rust1", since = "1.0.0")]
753744
pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
754745
core_str::StrExt::split_terminator(&self[..], pat)
@@ -778,22 +769,13 @@ impl str {
778769
///
779770
/// # Examples
780771
///
781-
/// Simple patterns:
782-
///
783772
/// ```
784773
/// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
785774
/// assert_eq!(v, ["B", "A"]);
786775
///
787776
/// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
788777
/// assert_eq!(v, ["", "B", "", "A"]);
789778
/// ```
790-
///
791-
/// More complex patterns with closures:
792-
///
793-
/// ```
794-
/// let v: Vec<&str> = "abc1def2ghi3".rsplit_terminator(|c: char| c.is_numeric()).collect();
795-
/// assert_eq!(v, ["ghi", "def", "abc"]);
796-
/// ```
797779
#[stable(feature = "rust1", since = "1.0.0")]
798780
pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
799781
where P::Searcher: ReverseSearcher<'a>
@@ -837,11 +819,11 @@ impl str {
837819
/// assert_eq!(v, [""]);
838820
/// ```
839821
///
840-
/// More complex patterns with closures:
822+
/// A more complex pattern, using a closure:
841823
///
842824
/// ```
843-
/// let v: Vec<&str> = "abc1def2ghi".splitn(2, |c: char| c.is_numeric()).collect();
844-
/// assert_eq!(v, ["abc", "def2ghi"]);
825+
/// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
826+
/// assert_eq!(v, ["abc", "defXghi"]);
845827
/// ```
846828
#[stable(feature = "rust1", since = "1.0.0")]
847829
pub fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
@@ -882,10 +864,10 @@ impl str {
882864
/// assert_eq!(v, ["leopard", "lion::tiger"]);
883865
/// ```
884866
///
885-
/// More complex patterns with closures:
867+
/// A more complex pattern, using a closure:
886868
///
887869
/// ```
888-
/// let v: Vec<&str> = "abc1def2ghi".rsplitn(2, |c: char| c.is_numeric()).collect();
870+
/// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
889871
/// assert_eq!(v, ["ghi", "abc1def"]);
890872
/// ```
891873
#[stable(feature = "rust1", since = "1.0.0")]
@@ -920,7 +902,7 @@ impl str {
920902
/// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
921903
/// assert_eq!(v, ["abc", "abc", "abc"]);
922904
///
923-
/// let v: Vec<&str> = "1abc2abc3".matches(|c: char| c.is_numeric()).collect();
905+
/// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
924906
/// assert_eq!(v, ["1", "2", "3"]);
925907
/// ```
926908
#[unstable(feature = "collections",
@@ -953,7 +935,7 @@ impl str {
953935
/// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
954936
/// assert_eq!(v, ["abc", "abc", "abc"]);
955937
///
956-
/// let v: Vec<&str> = "1abc2abc3".rmatches(|c: char| c.is_numeric()).collect();
938+
/// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
957939
/// assert_eq!(v, ["3", "2", "1"]);
958940
/// ```
959941
#[unstable(feature = "collections",
@@ -1199,15 +1181,16 @@ impl str {
11991181
///
12001182
/// ```
12011183
/// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1184+
/// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
12021185
///
12031186
/// let x: &[_] = &['1', '2'];
12041187
/// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
12051188
/// ```
12061189
///
1207-
/// More complex patterns with closures:
1190+
/// A more complex pattern, using a closure:
12081191
///
12091192
/// ```
1210-
/// assert_eq!("123foo1bar123".trim_matches(|c: char| c.is_numeric()), "foo1bar");
1193+
/// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
12111194
/// ```
12121195
#[stable(feature = "rust1", since = "1.0.0")]
12131196
pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
@@ -1224,20 +1207,13 @@ impl str {
12241207
///
12251208
/// # Examples
12261209
///
1227-
/// Simple patterns:
1228-
///
12291210
/// ```
12301211
/// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1212+
/// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
12311213
///
12321214
/// let x: &[_] = &['1', '2'];
12331215
/// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
12341216
/// ```
1235-
///
1236-
/// More complex patterns with closures:
1237-
///
1238-
/// ```
1239-
/// assert_eq!("123foo1bar123".trim_left_matches(|c: char| c.is_numeric()), "foo1bar123");
1240-
/// ```
12411217
#[stable(feature = "rust1", since = "1.0.0")]
12421218
pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
12431219
core_str::StrExt::trim_left_matches(&self[..], pat)
@@ -1255,14 +1231,16 @@ impl str {
12551231
///
12561232
/// ```
12571233
/// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1234+
/// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1235+
///
12581236
/// let x: &[_] = &['1', '2'];
12591237
/// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
12601238
/// ```
12611239
///
1262-
/// More complex patterns with closures:
1240+
/// A more complex pattern, using a closure:
12631241
///
12641242
/// ```
1265-
/// assert_eq!("123foo1bar123".trim_right_matches(|c: char| c.is_numeric()), "123foo1bar");
1243+
/// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
12661244
/// ```
12671245
#[stable(feature = "rust1", since = "1.0.0")]
12681246
pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
@@ -1499,7 +1477,7 @@ impl str {
14991477
/// ```
15001478
/// let s = "Löwe 老虎 Léopard";
15011479
///
1502-
/// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5));
1480+
/// assert_eq!(s.find(char::is_whitespace), Some(5));
15031481
/// assert_eq!(s.find(char::is_lowercase), Some(1));
15041482
/// ```
15051483
///
@@ -1541,7 +1519,7 @@ impl str {
15411519
/// ```
15421520
/// let s = "Löwe 老虎 Léopard";
15431521
///
1544-
/// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12));
1522+
/// assert_eq!(s.rfind(char::is_whitespace), Some(12));
15451523
/// assert_eq!(s.rfind(char::is_lowercase), Some(20));
15461524
/// ```
15471525
///

0 commit comments

Comments
 (0)