macro_rules! assert {
($($tokens:tt)*) => { ... };
}Expand description
Assert that an expression evaluates to true or matches a pattern.
Use a let expression to test an expression against a pattern: assert!(let pattern = expr).
For other tests, just give a boolean expression to the macro: assert!(1 + 2 == 2).
If the expression evaluates to false or if the pattern doesn’t match, an assertion failure is printed and the macro panics instantly.
Use check! if you still want further checks to be executed.
All placeholders in let patterns are made available in the calling scope.
Additionally, the macro supports let chains (regardless of your compiler version):
assert!(
let Err(e) = std::fs::File::open("/non/existing/file")
&& e.kind() == std::io::ErrorKind::NotFound
&& let Some(os_code) = e.raw_os_error()
);
println!("OS error code: {os_code}");§Custom messages
You can pass additional arguments to the macro. These will be used to print a custom message in addition to the normal message.
assert!(3 * 4 == 12, "Oh no, math is broken! 1 + 1 == {}", 1 + 1);