Macro faux::pattern[][src]

macro_rules! pattern {
    ($( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => { ... };
    ($ty:ty => $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => { ... };
}

Returns an ArgMatcher that succeeds if the provided pattern matches.

The returned Argmatcher implements fmt::Display using the string representation of the pattern.

This macro has two forms:

  • pattern!(pattern)
  • pattern!(type => pattern)

Use the latter if you need to be specific about the type being matched against.

use faux::{pattern, matcher::ArgMatcher};

// the type can be implicit
let is_alphabet = pattern!('A'..='Z' | 'a'..='z');
assert!(is_alphabet.matches(&'f'));
assert!(!is_alphabet.matches(&' '));

// or the type can be explicit
let exists_more_than_two = pattern!(Option<_> => Some(x) if *x > 2);
assert!(exists_more_than_two.matches(&Some(4)));
assert!(!exists_more_than_two.matches(&Some(1)));

println!("{}", exists_more_than_two); // 'Some(x) if *x > 2'