/// Assert that an expression matches a refutable pattern. /// /// Syntax: `assert_matches!(` *expression* `,` *pattern* `)` /// /// Panic with a message that shows the expression if it does not match the /// pattern. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate matches; /// /// fn main() { /// let data = [1, 2, 3]; /// assert_matches!(data.get(1), Some(_)); /// } /// ``` #[macro_export]
macro_rules! assert_matches {
($expression:expr, $($pattern:tt)+) => { match $expression {
$($pattern)+ => (), ref e => panic!("assertion failed: `{:?}` does not match `{}`", e, stringify!($($pattern)+)),
}
}
}
/// Assert that an expression matches a refutable pattern using debug assertions. /// /// Syntax: `debug_assert_matches!(` *expression* `,` *pattern* `)` /// /// If debug assertions are enabled, panic with a message that shows the /// expression if it does not match the pattern. /// /// When debug assertions are not enabled, this macro does nothing. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate matches; /// /// fn main() { /// let data = [1, 2, 3]; /// debug_assert_matches!(data.get(1), Some(_)); /// } /// ``` #[macro_export]
macro_rules! debug_assert_matches {
($expression:expr, $($pattern:tt)+) => { if cfg!(debug_assertions) { match $expression {
$($pattern)+ => (), ref e => panic!("assertion failed: `{:?}` does not match `{}`", e, stringify!($($pattern)+)),
}
}
}
}
#[test] fn matches_works() { let foo = Some("-12");
assert!(matches!(foo, Some(bar) if
matches!(bar.as_bytes()[0], b'+' | b'-') &&
matches!(bar.as_bytes()[1], b'0'...b'9')
));
}
#[test] fn assert_matches_works() { let foo = Some("-12");
assert_matches!(foo, Some(bar) if
matches!(bar.as_bytes()[0], b'+' | b'-') &&
matches!(bar.as_bytes()[1], b'0'...b'9')
);
}
#[test] #[should_panic(expected = "assertion failed: `Some(\"-AB\")` does not match ")] fn assert_matches_panics() { let foo = Some("-AB");
assert_matches!(foo, Some(bar) if
matches!(bar.as_bytes()[0], b'+' | b'-') &&
matches!(bar.as_bytes()[1], b'0'...b'9')
);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.