Macro faux::pattern [−][src]
macro_rules! pattern {
($($pattern : pat) | + $(if $guard : expr) ? $(,) ?) => { ... };
($ty : ty => $($pattern : pat) | + $(if $guard : expr) ? $(,) ?) => { ... };
}
Expand description
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.
Examples
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'
Usage within when!
faux::when!
does not have a special syntax for
this matcher. See the matcher
syntax for more
information.
ⓘ
// we can call it within `when!`
faux::when!(my_struct.some_method(_ = faux::pattern!(|_: &i32| true)))
.then_return(5);
// or call outside `when!`
faux::when!(my_struct.some_method)
.with_args((faux::pattern!(|_: &i32| true),)).then_return(5);