claims

Macro assert_matches

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

Asserts that the expression matches the provided pattern.

Works like the std::matches! macro, but 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 a custom message
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