Skip to main content

const_assert

Macro const_assert 

Source
macro_rules! const_assert {
    ($condition:expr $(,$arg:literal)?) => { ... };
}
Expand description

Assertion during constant evaluation.

This is a more powerful version of static_assert! that can refer to generics inside functions or implementation blocks. However, it also has a limitation where it can only appear in places where statements can appear; for example, you cannot use it as an item in the module.

static_assert! should be preferred if no generics are referred to in the condition. You cannot refer to variables with const_assert! (even inside const fn); if you need the capability, use build_assert!. See the module documentation.

ยงExamples

fn foo<const N: usize>() {
    const_assert!(N > 1);
}

fn bar<T>() {
    const_assert!(size_of::<T>() > 0, "T cannot be ZST");
}