macro_rules! assert_matches {
($expression:expr, $pattern:pat $(if $condition:expr)? $(,)?) => { ... };
($expression:expr, $pattern:pat $(if $condition:expr)? => $code:expr $(,)?) => { ... };
($expression:expr, $pattern:pat $(if $condition:expr)?, $($arg:tt)+) => { ... };
($expression:expr, $pattern:pat $(if $condition:expr)? => $code:expr, $($arg:tt)+) => { ... };
}
Expand description
Attempts to be a replacement for assert!(matches!(...))
but with better error messages,
and allowing further code on success.
Matches the assert_eq!
syntax for error messages.
let x = Some(42);
assert_matches!(x, Some(_));
assert_matches!(x, Some(x) => { assert_eq!(x, 42); });
ⓘ
assert_matches!(x, None, "Expected None, got {:?}", x);
ⓘ
assert_matches!(x, Some(x) => { assert_eq!(x, 41); }, "Expected Some(41), got {:?}", x);
§Alternatives
We can’t use the std assert_matches! macro, as it looks like it is doomed to be stuck unstabilized for the foreseeable future.
This takes some inspiration regarding the => {}
syntax from the
assert_matches
crate but the code had bugs with trailing commas and its error messages weren’t ideal.