assert_not!
A simple Rust macro that works like the inverse of assert!:
Passes if the condition is false, panics if the condition is true.
Overview
assert_not! is a macro for Rust that helps you verify that a condition does not hold.
If the given expression evaluates to false, the test passes.
If it evaluates to true, the macro panics with an optional error message.
Motivation
While Rust provides the standard assert! macro for asserting that a condition is true,
sometimes you need to ensure a condition is not true.
assert_not! fills this gap with clear intent and easy usage.
Usage
use assert_not;
let expression = false;
let value = 42;
assert_not!;
assert_not!;
Examples
use assert_not;
let value = 5;
assert_not!; // Passes unless value is 10
let foo: = None;
assert_not!; // Passes unless foo is Some
let x = 3;
assert_not!; // Passes unless x > 5, with custom message
Behavior
- If
expressionisfalse: Nothing happens, test passes. - If
expressionistrue: Panics, error message displayed (with optional formatting).
Implementation Notes
- Follows the standard
assert!macro's API style. - Can be extended for debug-only variants (e.g.
debug_assert_not!).
License
MIT or Apache-2.0
Feel free to contribute or open issues!