Skip to content

Commit 7a22cf6

Browse files
committed
Add examples to Pattern docs
1 parent cbe96b0 commit 7a22cf6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/liballoc/string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,12 @@ impl<'a> Extend<Cow<'a, str>> for String {
18281828
}
18291829

18301830
/// 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

+26
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
452452
impl<'a> DoubleEndedSearcher<'a> for CharSearcher<'a> {}
453453

454454
/// 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

@@ -697,6 +703,13 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for CharSliceSearcher<'a, 'b> {
697703
impl<'a, 'b> DoubleEndedSearcher<'a> for CharSliceSearcher<'a, 'b> {}
698704

699705
/// 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
}
@@ -739,6 +752,13 @@ where
739752
impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool {}
740753

741754
/// 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

0 commit comments

Comments
 (0)