What debug_assert_eq does

debug_assert_eq is a macro exposed by the std module of the Rust standard library. It is part of the public, stable API and you can rely on it from any edition of Rust.

debug_assert_eq! is a macro — its body is expanded at compile time, before the surrounding code is type-checked. Macros let std offer features that plain functions cannot: variadic arguments, format-string parsing, conditional compilation, and so on. The full canonical path is std::debug_assert_eq; bring it into scope with use std::debug_assert_eq; or refer to it by its full path.

When to use it

Invoke debug_assert_eq! when you need its compile-time expansion. Macros can do things plain functions cannot: format-string parsing, custom syntax, code generation. Refer to the std::debug_assert_eq docs for the exact invocation syntax.

Annotated examples

Invoking the debug_assert_eq! macro

fn demo() {
    // Macros are invoked with !, not (). The arguments inside the parens
    // (or square brackets, or braces) follow each macro's own syntax —
    // refer to the std::debug_assert_eq reference for examples.
    debug_assert_eq!();
}

Macros are evaluated at compile time and can do things ordinary functions cannot — variadic arguments, custom syntax, format-string parsing. The `!` after the name is the language tell that you're calling a macro.

For deeper background, see the canonical Rust patterns reference for the broader context behind this section.

Using debug_assert_eq! inside a larger expression

fn build_message(name: &str, count: u32) -> String {
    // Many std macros (format!, write!, vec!, etc.) integrate with format
    // strings: capture local bindings with {name} or pass values
    // positionally with {}.
    format!("hello {name} ({count}x via debug_assert_eq!)")
}

Many std macros — format!, vec!, println!, write!, assert! — are deeply integrated with the rest of the language. Inline-format-string capture (`{name}`) is one of the more recent ergonomic wins.

Common pitfalls

Macros expand to source code at compile time, so error messages can point inside the expansion rather than at the call site — `cargo expand` is invaluable for debugging unexpected behaviour. Macros also can't be passed around like functions; if you need a callable you can store, build a closure instead.

For deeper background, see an in-depth Rust idioms cheat sheet for the broader context behind this section.

Performance & threading notes

Macros expand at compile time and contribute zero runtime overhead beyond the cost of the code they expand to. They can, however, slow down compilation noticeably if used heavily — every macro invocation is parsed and expanded.