🔬This is a nightly-only experimental API. (pattern #27721)
Expand description

The string Pattern API.

The Pattern API provides a generic mechanism for using different pattern types when searching through a string.

For more details, see the traits Pattern, Searcher, ReverseSearcher, and DoubleEndedSearcher.

Although this API is unstable, it is exposed via stable APIs on the str type.

Examples

Pattern is implemented in the stable API for &str, char, slices of char, and functions and closures implementing FnMut(char) -> bool.

let s = "Can you find a needle in a haystack?";

// &str pattern
assert_eq!(s.find("you"), Some(4));
// char pattern
assert_eq!(s.find('n'), Some(2));
// array of chars pattern
assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u']), Some(1));
// slice of chars pattern
assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u'][..]), Some(1));
// closure pattern
assert_eq!(s.find(|c: char| c.is_ascii_punctuation()), Some(35));
Run

Macros

pattern_methods 🔒 Experimental
searcher_methods 🔒 Experimental

Structs

Associated type for <&[char; N] as Pattern<'a>>::Searcher.
CharArraySearcherExperimental
Associated type for <[char; N] as Pattern<'a>>::Searcher.
Associated type for <F as Pattern<'a>>::Searcher.
CharSearcherExperimental
Associated type for <char as Pattern<'a>>::Searcher.
CharSliceSearcherExperimental
Associated type for <&[char] as Pattern<'a>>::Searcher.
EmptyNeedle 🔒 Experimental
MultiCharEqPattern 🔒 Experimental
MultiCharEqSearcher 🔒 Experimental
StrSearcherExperimental
Associated type for <&str as Pattern<'a>>::Searcher.
TwoWaySearcher 🔒 Experimental
The internal state of the two-way substring search algorithm.

Enums

MatchOnly 🔒 Experimental
Skip to match intervals as quickly as possible
RejectAndMatch 🔒 Experimental
Emit Rejects regularly
StrSearcherImpl 🔒 Experimental

Traits

A marker trait to express that a ReverseSearcher can be used for a DoubleEndedIterator implementation.
MultiCharEq 🔒 Experimental
PatternExperimental
A string pattern.
ReverseSearcherExperimental
A reverse searcher for a string pattern.
SearcherExperimental
A searcher for a string pattern.
TwoWayStrategy 🔒 Experimental