# Compile time assert

**URL:** <https://internals.rust-lang.org/t/compile-time-assert/6751>\
**Category:** compiler\
**Created:** [February 12, 2018, 12:17pm UTC](https://internals.rust-lang.org/t/compile-time-assert/6751 "2018-02-12T12:17:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davemilter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davemilter/32/3493_2.png) [@davemilter](https://internals.rust-lang.org/u/davemilter)\
**Post date:** [February 12, 2018, 12:17pm UTC](https://internals.rust-lang.org/t/compile-time-assert/6751/1 "2018-02-12T12:17:52Z")

</div>

I’ve read this: [https://github.com/rust-lang/rfcs/pull/1229](https://github.com/rust-lang/rfcs/pull/1229)

and wonder am I understand it correctly, if at now I write:

```rust
assert_eq!(4, mem::size_of::<usize>());

```

then after implementing [1229](https://github.com/rust-lang/rfcs/pull/1229) I got at least warning during compile time.

So at now I can using assert macros and check constraints at runtime, and after somebody implement [1229](https://github.com/rust-lang/rfcs/pull/1229) my `assert_eq!` magically become compile time asserts?

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [February 12, 2018, 5:38pm UTC](https://internals.rust-lang.org/t/compile-time-assert/6751/2 "2018-02-12T17:38:19Z")

</div>

You can abuse transmute to do this at compile time if you want:

```rust
fn _size_check() {
    unsafe { mem::transmute::<usize, [u8; 4]>(0); }
}

```

---

<div class="post-metadata">

**Author:** ![davemilter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davemilter/32/3493_2.png) [@davemilter](https://internals.rust-lang.org/u/davemilter)\
**Post date:** [February 12, 2018, 11:46pm UTC](https://internals.rust-lang.org/t/compile-time-assert/6751/3 "2018-02-12T23:46:20Z")

</div>

> [@sfackler](#):
>
> You can abuse transmute to do this at compile time if you want:
> 
> fn \_size\_check() { unsafe { mem::transmute::\<usize, [u8; 4]\>(0); } }

Yes, I know about this trick, there is also [static\_assertions crate](https://crates.io/crates/static_assertions) that provides nice wrapper around this or similar trick.

But it would be great to have it inside `std` or compiler.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/compile-time-assert/6751/4 "2019-03-25T08:29:39Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
