Trait Pattern

Source
pub trait Pattern: Sized {
    type Searcher<'a>: Searcher<'a>;

    // Required method
    fn into_searcher(self, haystack: &str) -> Self::Searcher<'_>;

    // Provided methods
    fn is_contained_in(self, haystack: &str) -> bool { ... }
    fn is_prefix_of(self, haystack: &str) -> bool { ... }
    fn is_suffix_of<'a>(self, haystack: &'a str) -> bool
       where Self::Searcher<'a>: ReverseSearcher<'a> { ... }
    fn strip_prefix_of(self, haystack: &str) -> Option<&str> { ... }
    fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
       where Self::Searcher<'a>: ReverseSearcher<'a> { ... }
    fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>> { ... }
}
๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)
Expand description

A string pattern.

A Pattern expresses that the implementing type can be used as a string pattern for searching in a &str.

For example, both 'a' and "aa" are patterns that would match at index 1 in the string "baaaab".

The trait itself acts as a builder for an associated Searcher type, which does the actual work of finding occurrences of the pattern in a string.

Depending on the type of the pattern, the behavior of methods like str::find and str::contains can change. The table below describes some of those behaviors.

Pattern typeMatch condition
&stris substring
charis contained in string
&[char]any char in slice is contained in string
F: FnMut(char) -> boolF returns true for a char in string
&&stris substring
&Stringis substring

ยงExamples

// &str
assert_eq!("abaaa".find("ba"), Some(1));
assert_eq!("abaaa".find("bac"), None);

// char
assert_eq!("abaaa".find('a'), Some(0));
assert_eq!("abaaa".find('b'), Some(1));
assert_eq!("abaaa".find('c'), None);

// &[char; N]
assert_eq!("ab".find(&['b', 'a']), Some(0));
assert_eq!("abaaa".find(&['a', 'z']), Some(0));
assert_eq!("abaaa".find(&['c', 'd']), None);

// &[char]
assert_eq!("ab".find(&['b', 'a'][..]), Some(0));
assert_eq!("abaaa".find(&['a', 'z'][..]), Some(0));
assert_eq!("abaaa".find(&['c', 'd'][..]), None);

// FnMut(char) -> bool
assert_eq!("abcdef_z".find(|ch| ch > 'd' && ch < 'y'), Some(4));
assert_eq!("abcddd_z".find(|ch| ch > 'd' && ch < 'y'), None);

Required Associated Typesยง

Source

type Searcher<'a>: Searcher<'a>

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Associated searcher for this pattern

Required Methodsยง

Source

fn into_searcher(self, haystack: &str) -> Self::Searcher<'_>

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Constructs the associated searcher from self and the haystack to search in.

Provided Methodsยง

Source

fn is_contained_in(self, haystack: &str) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Checks whether the pattern matches anywhere in the haystack

Source

fn is_prefix_of(self, haystack: &str) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Checks whether the pattern matches at the front of the haystack

Source

fn is_suffix_of<'a>(self, haystack: &'a str) -> bool
where Self::Searcher<'a>: ReverseSearcher<'a>,

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Checks whether the pattern matches at the back of the haystack

Source

fn strip_prefix_of(self, haystack: &str) -> Option<&str>

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Removes the pattern from the front of haystack, if it matches.

Source

fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
where Self::Searcher<'a>: ReverseSearcher<'a>,

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Removes the pattern from the back of haystack, if it matches.

Source

fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>>

๐Ÿ”ฌThis is a nightly-only experimental API. (pattern #27721)

Returns the pattern as utf-8 bytes if possible.

Dyn Compatibilityยง

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementorsยง

Sourceยง

impl Pattern for char

Searches for chars that are equal to a given char.

ยงExamples

assert_eq!("Hello world".find('o'), Some(4));
Sourceยง

impl<'b> Pattern for &'b str

Non-allocating substring search.

Will handle the pattern "" as returning empty matches at each character boundary.

ยงExamples

assert_eq!("Hello world".find("world"), Some(6));
Sourceยง

impl<'b> Pattern for &'b String

A convenience impl that delegates to the impl for &str.

ยงExamples

assert_eq!(String::from("Hello world").find("world"), Some(6));
Sourceยง

type Searcher<'a> = <&'b str as Pattern>::Searcher<'a>

Sourceยง

impl<'b> Pattern for &'b [char]

Searches for chars that are equal to any of the chars in the slice.

ยงExamples

assert_eq!("Hello world".find(&['o', 'l'][..]), Some(2));
assert_eq!("Hello world".find(&['h', 'w'][..]), Some(6));
Sourceยง

impl<'b, 'c> Pattern for &'c &'b str

Delegates to the &str impl.

Sourceยง

impl<'b, const N: usize> Pattern for &'b [char; N]

Searches for chars that are equal to any of the chars in the array.

ยงExamples

assert_eq!("Hello world".find(&['o', 'l']), Some(2));
assert_eq!("Hello world".find(&['h', 'w']), Some(6));
Sourceยง

impl<F> Pattern for F
where F: FnMut(char) -> bool,

Searches for chars that match the given predicate.

ยงExamples

assert_eq!("Hello world".find(char::is_uppercase), Some(0));
assert_eq!("Hello world".find(|c| "aeiou".contains(c)), Some(1));
Sourceยง

impl<const N: usize> Pattern for [char; N]

Searches for chars that are equal to any of the chars in the array.

ยงExamples

assert_eq!("Hello world".find(['o', 'l']), Some(2));
assert_eq!("Hello world".find(['h', 'w']), Some(6));