RFC 1653: assert_ne

libs (panic | test | assertions)

Summary

assert_ne is a macro that takes 2 arguments and panics if they are equal. It works and is implemented identically to assert_eq and serves as its complement. This proposal also includes a debug_asset_ne, matching debug_assert_eq.

Motivation

This feature, among other reasons, makes testing more readable and consistent as it complements asset_eq. It gives the same style panic message as assert_eq, which eliminates the need to write it yourself.

Detailed design

This feature has exactly the same design and implementation as assert_eq.

Here is the definition:

macro_rules! assert_ne {
    ($left:expr , $right:expr) => ({
        match (&$left, &$right) {
            (left_val, right_val) => {
                if *left_val == *right_val {
                    panic!("assertion failed: `(left != right)` \
                           (left: `{:?}`, right: `{:?}`)", left_val, right_val)
                }
            }
        }
    })
}

This is complemented by a debug_assert_ne (similar to debug_assert_eq):

macro_rules! debug_assert_ne {
    ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_ne!($($arg)*); })
}

Drawbacks

Any addition to the standard library will need to be maintained forever, so it is worth weighing the maintenance cost of this over the value add. Given that it is so similar to assert_eq, I believe the weight of this drawback is low.

Alternatives

Alternatively, users implement this feature themselves, or use the crate assert_ne that I published.

Unresolved questions

None at this moment.