Macro static_assertions::const_assert [] [src]

macro_rules! const_assert {
    ($($xs:expr),+ $(,)*) => { ... };
    ($label:ident; $($xs:tt)+) => { ... };
}

Asserts that constant expressions evaluate to true.

There also exists const_assert_eq for validating whether a sequence of expressions are equal to one another.

Examples

Constant expressions can be ensured to have certain properties via this macro If the expression evaluates to false, the file will fail to compile. This is synonymous to static_assert in C++.

As a limitation, a unique label is required if the macro is used outside of a function.

const FIVE: usize = 5;

const_assert!(twenty_five; FIVE * FIVE == 25);

fn main() {
    const_assert!(2 + 2 == 4);
    const_assert!(FIVE - FIVE == 0);
}

Some expressions are blatantly false:

This code doesn't compile so be extra careful!
const_assert!(1 >= 2);