Skip to content

Commit 0335a94

Browse files
committed
doc: improve some of str examples
1 parent 474c6e0 commit 0335a94

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

src/libcollections/str.rs

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,20 @@ impl str {
636636
///
637637
/// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
638638
/// assert_eq!(v, ["lion", "tiger", "leopard"]);
639-
/// ```
640-
///
641-
/// More complex patterns with closures:
642639
///
643-
/// ```
644-
/// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect();
640+
/// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
645641
/// assert_eq!(v, ["abc", "def", "ghi"]);
646642
///
647643
/// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
648644
/// assert_eq!(v, ["lion", "tiger", "leopard"]);
649645
/// ```
646+
///
647+
/// A more complex pattern, using a closure:
648+
///
649+
/// ```
650+
/// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
651+
/// assert_eq!(v, ["abc", "def", "ghi"]);
652+
/// ```
650653
#[stable(feature = "rust1", since = "1.0.0")]
651654
pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
652655
core_str::StrExt::split(&self[..], pat)
@@ -687,14 +690,11 @@ impl str {
687690
/// assert_eq!(v, ["leopard", "tiger", "lion"]);
688691
/// ```
689692
///
690-
/// More complex patterns with closures:
693+
/// A more complex pattern, using a closure:
691694
///
692-
/// ```rust
693-
/// let v: Vec<&str> = "abc1def2ghi".rsplit(|c: char| c.is_numeric()).collect();
695+
/// ```
696+
/// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
694697
/// assert_eq!(v, ["ghi", "def", "abc"]);
695-
///
696-
/// let v: Vec<&str> = "lionXtigerXleopard".rsplit(char::is_uppercase).collect();
697-
/// assert_eq!(v, ["leopard", "tiger", "lion"]);
698698
/// ```
699699
#[stable(feature = "rust1", since = "1.0.0")]
700700
pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
@@ -729,22 +729,13 @@ impl str {
729729
///
730730
/// # Examples
731731
///
732-
/// Simple patterns:
733-
///
734732
/// ```
735733
/// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
736734
/// assert_eq!(v, ["A", "B"]);
737735
///
738736
/// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
739737
/// assert_eq!(v, ["A", "", "B", ""]);
740738
/// ```
741-
///
742-
/// More complex patterns with closures:
743-
///
744-
/// ```
745-
/// let v: Vec<&str> = "abc1def2ghi3".split_terminator(|c: char| c.is_numeric()).collect();
746-
/// assert_eq!(v, ["abc", "def", "ghi"]);
747-
/// ```
748739
#[stable(feature = "rust1", since = "1.0.0")]
749740
pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
750741
core_str::StrExt::split_terminator(&self[..], pat)
@@ -774,22 +765,13 @@ impl str {
774765
///
775766
/// # Examples
776767
///
777-
/// Simple patterns:
778-
///
779768
/// ```
780769
/// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
781770
/// assert_eq!(v, ["B", "A"]);
782771
///
783772
/// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
784773
/// assert_eq!(v, ["", "B", "", "A"]);
785774
/// ```
786-
///
787-
/// More complex patterns with closures:
788-
///
789-
/// ```
790-
/// let v: Vec<&str> = "abc1def2ghi3".rsplit_terminator(|c: char| c.is_numeric()).collect();
791-
/// assert_eq!(v, ["ghi", "def", "abc"]);
792-
/// ```
793775
#[stable(feature = "rust1", since = "1.0.0")]
794776
pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
795777
where P::Searcher: ReverseSearcher<'a>
@@ -833,11 +815,11 @@ impl str {
833815
/// assert_eq!(v, [""]);
834816
/// ```
835817
///
836-
/// More complex patterns with closures:
818+
/// A more complex pattern, using a closure:
837819
///
838820
/// ```
839-
/// let v: Vec<&str> = "abc1def2ghi".splitn(2, |c: char| c.is_numeric()).collect();
840-
/// assert_eq!(v, ["abc", "def2ghi"]);
821+
/// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
822+
/// assert_eq!(v, ["abc", "defXghi"]);
841823
/// ```
842824
#[stable(feature = "rust1", since = "1.0.0")]
843825
pub fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
@@ -878,10 +860,10 @@ impl str {
878860
/// assert_eq!(v, ["leopard", "lion::tiger"]);
879861
/// ```
880862
///
881-
/// More complex patterns with closures:
863+
/// A more complex pattern, using a closure:
882864
///
883865
/// ```
884-
/// let v: Vec<&str> = "abc1def2ghi".rsplitn(2, |c: char| c.is_numeric()).collect();
866+
/// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
885867
/// assert_eq!(v, ["ghi", "abc1def"]);
886868
/// ```
887869
#[stable(feature = "rust1", since = "1.0.0")]
@@ -916,7 +898,7 @@ impl str {
916898
/// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
917899
/// assert_eq!(v, ["abc", "abc", "abc"]);
918900
///
919-
/// let v: Vec<&str> = "1abc2abc3".matches(|c: char| c.is_numeric()).collect();
901+
/// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
920902
/// assert_eq!(v, ["1", "2", "3"]);
921903
/// ```
922904
#[unstable(feature = "collections",
@@ -949,7 +931,7 @@ impl str {
949931
/// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
950932
/// assert_eq!(v, ["abc", "abc", "abc"]);
951933
///
952-
/// let v: Vec<&str> = "1abc2abc3".rmatches(|c: char| c.is_numeric()).collect();
934+
/// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
953935
/// assert_eq!(v, ["3", "2", "1"]);
954936
/// ```
955937
#[unstable(feature = "collections",
@@ -1195,15 +1177,16 @@ impl str {
11951177
///
11961178
/// ```
11971179
/// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1180+
/// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
11981181
///
11991182
/// let x: &[_] = &['1', '2'];
12001183
/// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
12011184
/// ```
12021185
///
1203-
/// More complex patterns with closures:
1186+
/// A more complex pattern, using a closure:
12041187
///
12051188
/// ```
1206-
/// assert_eq!("123foo1bar123".trim_matches(|c: char| c.is_numeric()), "foo1bar");
1189+
/// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
12071190
/// ```
12081191
#[stable(feature = "rust1", since = "1.0.0")]
12091192
pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
@@ -1220,20 +1203,13 @@ impl str {
12201203
///
12211204
/// # Examples
12221205
///
1223-
/// Simple patterns:
1224-
///
12251206
/// ```
12261207
/// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1208+
/// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
12271209
///
12281210
/// let x: &[_] = &['1', '2'];
12291211
/// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
12301212
/// ```
1231-
///
1232-
/// More complex patterns with closures:
1233-
///
1234-
/// ```
1235-
/// assert_eq!("123foo1bar123".trim_left_matches(|c: char| c.is_numeric()), "foo1bar123");
1236-
/// ```
12371213
#[stable(feature = "rust1", since = "1.0.0")]
12381214
pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
12391215
core_str::StrExt::trim_left_matches(&self[..], pat)
@@ -1251,14 +1227,16 @@ impl str {
12511227
///
12521228
/// ```
12531229
/// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1230+
/// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1231+
///
12541232
/// let x: &[_] = &['1', '2'];
12551233
/// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
12561234
/// ```
12571235
///
1258-
/// More complex patterns with closures:
1236+
/// A more complex pattern, using a closure:
12591237
///
12601238
/// ```
1261-
/// assert_eq!("123foo1bar123".trim_right_matches(|c: char| c.is_numeric()), "123foo1bar");
1239+
/// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
12621240
/// ```
12631241
#[stable(feature = "rust1", since = "1.0.0")]
12641242
pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
@@ -1494,7 +1472,7 @@ impl str {
14941472
/// ```
14951473
/// let s = "Löwe 老虎 Léopard";
14961474
///
1497-
/// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5));
1475+
/// assert_eq!(s.find(char::is_whitespace), Some(5));
14981476
/// assert_eq!(s.find(char::is_lowercase), Some(1));
14991477
/// ```
15001478
///
@@ -1536,7 +1514,7 @@ impl str {
15361514
/// ```
15371515
/// let s = "Löwe 老虎 Léopard";
15381516
///
1539-
/// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12));
1517+
/// assert_eq!(s.rfind(char::is_whitespace), Some(12));
15401518
/// assert_eq!(s.rfind(char::is_lowercase), Some(20));
15411519
/// ```
15421520
///

0 commit comments

Comments
 (0)