macro_rules! assert_matches {
    ($expression:expr, $( $pattern:pat )|+) => { ... };
    ($expression:expr, $( $pattern:pat )|+ if $guard: expr) => { ... };
    ($expression:expr, $( $pattern:pat )|+, $($arg:tt)+) => { ... };
    ($expression:expr, $( $pattern:pat )|+ if $guard: expr, $($arg:tt)+) => { ... };
}
Expand description

Asserts that expression matches any of the given variants.

This macro is available for Rust 1.26+.

It works exactly as std::matches! macro, except it panics if there is no match.

Uses

Assertions are always checked in both debug and release builds, and cannot be disabled. See debug_assert_matches! for assertions that are not enabled in release builds by default.

Custom messages

This macro has a second form, where a custom panic message can be provided with or without arguments for formatting. See std::fmt for syntax for this form.

Examples

let foo = 'f';
assert_matches!(foo, 'A'..='Z' | 'a'..='z');

// With custom messages
assert_matches!(foo, 'A'..='Z' | 'a'..='z', "expecting it to be letter: {}", foo);
let bar: Option<i32> = None;
assert_matches!(bar, Some(x) if x > 2);  // Will panic