MatchOne

Trait MatchOne 

Source
pub trait MatchOne: FirstElem {
    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"));

Using macros for better performance:

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§

Source

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"));

Provided Methods§

Implementations on Foreign Types§

Source§

impl MatchOne for char

Source§

type Pattern = str

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl MatchOne for str

Source§

type Pattern = str

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl MatchOne for u8

Source§

type Pattern = [u8]

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl MatchOne for ()

Source§

type Pattern = ()

Source§

fn match_one(&self, _pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl<T: MatchOne + ?Sized> MatchOne for &T

Source§

type Pattern = <T as MatchOne>::Pattern

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl<T: MatchOne + ?Sized> MatchOne for &mut T

Source§

type Pattern = <T as MatchOne>::Pattern

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl<T: MatchOne> MatchOne for Option<T>

Source§

type Pattern = <T as MatchOne>::Pattern

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl<T: MatchOne> MatchOne for [T]

Source§

type Pattern = <T as MatchOne>::Pattern

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Source§

impl<T: MatchOne, const N: usize> MatchOne for [T; N]

Source§

type Pattern = <T as MatchOne>::Pattern

Source§

fn match_one(&self, pattern: &Self::Pattern) -> Option<Self::Out>

Implementors§