Skip to content

Commit eb49f7f

Browse files
authored
Rollup merge of #71097 - pickfire:pattern-docs, r=Dylan-DPC
Pattern docs https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html#implementors
2 parents fd1b057 + 7a22cf6 commit eb49f7f

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/liballoc/string.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,13 @@ impl<'a> Extend<Cow<'a, str>> for String {
18271827
}
18281828
}
18291829

1830-
/// A convenience impl that delegates to the impl for `&str`
1830+
/// A convenience impl that delegates to the impl for `&str`.
1831+
///
1832+
/// # Examples
1833+
///
1834+
/// ```
1835+
/// assert_eq!(String::from("Hello world").find("world"), Some(6));
1836+
/// ```
18311837
#[unstable(
18321838
feature = "pattern",
18331839
reason = "API not fully fleshed out and ready to be stabilized",

src/libcore/str/pattern.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,13 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
451451

452452
impl<'a> DoubleEndedSearcher<'a> for CharSearcher<'a> {}
453453

454-
/// Searches for chars that are equal to a given char
454+
/// Searches for chars that are equal to a given `char`.
455+
///
456+
/// # Examples
457+
///
458+
/// ```
459+
/// assert_eq!("Hello world".find('o'), Some(4));
460+
/// ```
455461
impl<'a> Pattern<'a> for char {
456462
type Searcher = CharSearcher<'a>;
457463

@@ -696,7 +702,14 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for CharSliceSearcher<'a, 'b> {
696702

697703
impl<'a, 'b> DoubleEndedSearcher<'a> for CharSliceSearcher<'a, 'b> {}
698704

699-
/// Searches for chars that are equal to any of the chars in the array
705+
/// Searches for chars that are equal to any of the chars in the array.
706+
///
707+
/// # Examples
708+
///
709+
/// ```
710+
/// assert_eq!("Hello world".find(&['l', 'l'] as &[_]), Some(2));
711+
/// assert_eq!("Hello world".find(&['l', 'l'][..]), Some(2));
712+
/// ```
700713
impl<'a, 'b> Pattern<'a> for &'b [char] {
701714
pattern_methods!(CharSliceSearcher<'a, 'b>, MultiCharEqPattern, CharSliceSearcher);
702715
}
@@ -738,7 +751,14 @@ where
738751

739752
impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool {}
740753

741-
/// Searches for chars that match the given predicate
754+
/// Searches for chars that match the given predicate.
755+
///
756+
/// # Examples
757+
///
758+
/// ```
759+
/// assert_eq!("Hello world".find(char::is_uppercase), Some(0));
760+
/// assert_eq!("Hello world".find(|c| "aeiou".contains(c)), Some(1));
761+
/// ```
742762
impl<'a, F> Pattern<'a> for F
743763
where
744764
F: FnMut(char) -> bool,
@@ -763,6 +783,12 @@ impl<'a, 'b, 'c> Pattern<'a> for &'c &'b str {
763783
///
764784
/// Will handle the pattern `""` as returning empty matches at each character
765785
/// boundary.
786+
///
787+
/// # Examples
788+
///
789+
/// ```
790+
/// assert_eq!("Hello world".find("world"), Some(6));
791+
/// ```
766792
impl<'a, 'b> Pattern<'a> for &'b str {
767793
type Searcher = StrSearcher<'a, 'b>;
768794

@@ -771,7 +797,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
771797
StrSearcher::new(haystack, self)
772798
}
773799

774-
/// Checks whether the pattern matches at the front of the haystack
800+
/// Checks whether the pattern matches at the front of the haystack.
775801
#[inline]
776802
fn is_prefix_of(self, haystack: &'a str) -> bool {
777803
haystack.as_bytes().starts_with(self.as_bytes())
@@ -788,7 +814,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
788814
}
789815
}
790816

791-
/// Checks whether the pattern matches at the back of the haystack
817+
/// Checks whether the pattern matches at the back of the haystack.
792818
#[inline]
793819
fn is_suffix_of(self, haystack: &'a str) -> bool {
794820
haystack.as_bytes().ends_with(self.as_bytes())

0 commit comments

Comments
 (0)