Macro konst::assertc_ne
source · macro_rules! assertc_ne { ($left:expr, $right:expr $(, $($fmt:tt)* )? ) => { ... }; }
Expand description
For asserting that two values are unequal.
This macro is only evaluated at compile-time if used in a context that requires it (eg: in the expression assigned to a const _: () =
)
§Formatting
This uses the same syntax for formatting arguments as const_panic::concat_panic
.
By default, this only supports primitive types as arguments, to format arrays or custom types you must enable const_panic
’s "non_basic"
feature.
To pass user-defined types, they must implement both of these traits as described in their docs:
§Examples
§Unique strings
assert_eq!(NAMES, ["bob", "matt", "rob"]);
const NAMES: &[&str] = assert_unique(&["bob", "matt", "rob"]);
#[track_caller]
const fn assert_unique<'a, 'b>(names: &'a [&'b str]) -> &'a [&'b str] {
konst::for_range!{x in 0..names.len() =>
konst::for_range!{y in 0..names.len() =>
if x == y { continue }
konst::assertc_ne!{
names[x],
names[y],
"equal names at index `", x, "` and `", y, "`"
}
}
}
names
}
If the argument had repeated strings, this would be the error:
error[E0080]: evaluation of constant value failed
--> src/macros/assert_macros.rs:126:24
|
6 | const NAMES: &[&str] = assert_unique(&["bob", "matt", "rob", "rob"]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed: LEFT != RIGHT
left: `"rob"`
right: `"rob"`
: equal names at index `2` and `3`', src/macros/assert_macros.rs:6:24