Crate more_asserts[−][src]
Small library providing some macros helpful for asserting. The API is very
similar to the API provided by the stdlib’s own assert_eq!
, assert_ne!
,
debug_assert_eq!
, or debug_assert_ne!
.
Name | Enabled | Equivalent to |
---|---|---|
assert_le! | Always | assert!(a <= b) |
assert_lt! | Always | assert!(a < b) |
assert_ge! | Always | assert!(a >= b) |
assert_gt! | Always | assert!(a > b) |
debug_assert_le! | if cfg!(debug_assertions) | debug_assert!(a <= b) |
debug_assert_lt! | if cfg!(debug_assertions) | debug_assert!(a < b) |
debug_assert_ge! | if cfg!(debug_assertions) | debug_assert!(a >= b) |
debug_assert_gt! | if cfg!(debug_assertions) | debug_assert!(a > b) |
debug_unreachable! | if cfg!(debug_assertions) | unreachable! when debug_assertions are on. |
When one of the assertions fails, it prints out a message like the following:
thread 'main' panicked at 'assertion failed: `left < right`
left: `4`,
right: `3`', src/main.rs:47:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
Example
#[macro_use] extern crate more_asserts; #[derive(Debug, PartialEq, PartialOrd)] enum Example { Foo, Bar } fn main() { assert_le!(3, 4); assert_ge!(10, 10, "You can pass a message too (just like `assert_eq!`)"); debug_assert_lt!(1.3, 4.5, "Format syntax is supported ({}).", "also like `assert_eq!`"); assert_gt!(Example::Bar, Example::Foo, "It works on anything that implements PartialOrd, PartialEq, and Debug!"); }
Macros
assert_ge | Panics if the first expression is not greater than or equal to the second.
Requires that the values be comparable with |
assert_gt | Panics if the first expression is not strictly greater than the second.
Requires that the values be comparable with |
assert_le | Panics if the first expression is not less than or equal to the second.
Requires that the values be comparable with |
assert_lt | Panics if the first expression is not strictly less than the second.
Requires that the values be comparable with |
debug_assert_ge | Same as |
debug_assert_gt | Same as |
debug_assert_le | Same as |
debug_assert_lt | Same as |
debug_unreachable | Panics if reached. This is a variant of the standard library’s |