# Nicer static assertions

**URL:** <https://internals.rust-lang.org/t/nicer-static-assertions/15986>\
**Category:** language design\
**Created:** [January 18, 2022, 10:36pm UTC](https://internals.rust-lang.org/t/nicer-static-assertions/15986 "2022-01-18T22:36:53Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 18, 2022, 11:04pm UTC](https://internals.rust-lang.org/t/nicer-static-assertions/15986/2 "2022-01-18T23:04:23Z")

</div>

You can [already just write](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8dcc0471ee46b0179c987da7a8b44a84)

```rust
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

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

```

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

> [@bascule](#):
>
> ...but this is one of those things that's tiny enough and fundamental enough that I don't really want to pull in an external crate to define it

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

> **[static\_assertions](https://lib.rs/crates/static_assertions)**
>
> Compile-time assertions to ensure that invariants are met

---

_[View the full topic](https://internals.rust-lang.org/t/nicer-static-assertions/15986)._
