Macro const_format::assertc

source ·
macro_rules! assertc {
    ($($parameters:tt)*) => { ... };
}
Available on crate feature assertc only.
Expand description

Compile-time assertions with formatting.

This macro requires the "assertc" feature to be exported.

This macro uses the same syntax for the format string and supports the same formatting arguments as the formatc macro.

§Limitations

This macro has these limitations:

§Examples

§Passing assertion

#![feature(const_mut_refs)]

use const_format::assertc;

use std::mem::size_of;

assertc!(
    size_of::<&str>() == size_of::<&[u8]>(),
    "The size of `&str`({} bytes) and `&[u8]`({} bytes) aren't the same?!?!",
    size_of::<&str>(),
    size_of::<&[u8]>(),
);

§Failing assertion

This example demonstrates a failing assertion, and how the compiler error looks like as of 2023-10-14.

#![feature(const_mut_refs)]

use const_format::assertc;

const L: u64 = 2;
const R: u64 = 2;

assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L,  R);

This is the compiler output:

error[E0080]: evaluation of constant value failed
  --> const_format/src/macros/assertions/assertc_macros.rs:109:10
   |
11 | assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L,  R);
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
assertion failed.
2 plus 2 isn't 5, buddy
', const_format/src/macros/assertions/assertc_macros.rs:11:10