I experimented with trying to implement something like this with macro_rules!.
With an implementation like:
macro_rules! assert2 {
($left:tt == $right:tt) => ({
match (&$left, &$right) {
(left_val, right_val) => if !(*left_val == *right_val) {
panic!("{0} == {1} was false\n\t{0} = {2:?}, {1} = {3:?}",
stringify!($left), stringify!($right),
left_val, right_val);
}
}
})
}
Then assert2!( 5 == 2 + 3) fails to compile with “no rules expected the token +”. If I change left and right to be expr instead of tt, then it still fails with "$left:expr is followed by ==, which is not allowed for expr fragments".
Wrapping the 2 + 3 in parenthesis does work with with the tt version, but that is a little awkward and non-intuitive to use.
I suppose it is possible to build something that would handle arbitrary expressions, but as far as I understand it, it would have to essentially parse the expressions themselves, duplicating a significant portion of rust’s expression grammar.