Better_Software_Header_Mobile Better_Software_Header_Web

Find what you need - explore our website and developer resources

Static Assertions in Rust

Implementing Compile-Time Checks in Rust with a Simple Macro

constexpr int A = 6;

static_assert(A == 6);
macro_rules! const_assert {
    ($x:expr) => {
        const _: () = ::core::assert!($x);
    };
}
macro_rules! const_assert
($x:expr) => {
const _: () = ::core::assert!($x);
const A: i32 = 6;

const_assert!(A == 6);
const A: i32 = 6;

const _: () = assert!(A == 6);
const A: i32 = 6;

const B: i32 = 5;

const_assert!(B == 6);

fn main() {
    const_assert!(A == 6);

    println!("{A}");
}

Tags:

rust

About KDAB

1 Comment

20 - Nov - 2025

Vorpal

JoshuaGoins

Joshua Goins

Software Engineer

Sign up for the KDAB Newsletter

Learn Rust

Learn more