pub trait MatchOne {
type Out;
type Pattern: ?Sized;
// Required method
fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>;
// Provided method
fn is_match_one(&self, pattern: &Self::Pattern) -> bool { ... }
}Expand description
Simple matching of one char or byte, similar to regex
Lightweight and easy to use, and can work conveniently on u8 and Option
§Examples
use char_classes::any;
assert!(any("ab", 'a'));
assert!(any("ab", 'b'));
assert!(any("a-c", 'a'));
assert!(any("a-c", 'b'));
assert!(any("a-ce-f", 'e'));
assert!(any("a-ce-", '-'));
assert!(any("a-ce-", 'e'));
assert!(any("a-c", Some('b')));
assert!(any("a-c", ['b', 'd']));
assert!(any("a-c", "bd"));
assert!(! any("a-c", '-'));
assert!(! any("a-ce-", 'f'));
assert!(! any("a-c", None::<char>));
assert!(! any("a-c", ['d', 'b']));
assert!(! any("a-c", "db"));Match byte
use char_classes::any;
assert!(any(b"ab", b'a'));
assert!(any(b"ab", b'b'));
assert!(any(b"ab", b"bd"));
assert!(! any(b"ab", b'c'));
assert!(! any(b"ab", b"db"));Required Associated Types§
Required Methods§
Sourcefn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>
fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>
Match one element or first element
Match '-' Please write at the beginning or end, e.g "a-z-"
§Examples
assert_eq!(Some('a'), 'a'.match_one("a-c"));
assert_eq!(Some('b'), 'b'.match_one("a-c"));
assert_eq!(None, '-'.match_one("a-c"));
assert_eq!(Some('-'), '-'.match_one("a-"));
assert_eq!(Some('a'), "ab".match_one("a"));
assert_eq!(None, "ab".match_one("b"));