Nicer static assertions

You can already just write

const _: () = assert!(FOO == BAR, "assert_foo_equals_bar");

with no need for the function.

There's no way for a macro to know if it's used in expression or item position (and there shouldn't be), so the only way to make bare assert! work would be to make statements in item position evaluated at const time. This is probably not the best idea, as it's likely to be confused with script-style execution.

It probably is worth adding a const_assert! to the stdlib, as

macro_rules! const_assert {
    ($($tt:tt)*) => {
        const _: () = assert!($($tt)*);
    }
}

(perhaps with assert!'s match arms instead of a pass-through catch-all).

Just for linking's sake, here's the crate that does so (with pre-const-panic methods):

17 Likes