← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 4 of 8

Assert Macros

Assert macros are little tripwires in your code that stop everything and complain loudly if something isn't true.
Syntax
rust
assert!(condition);
assert_eq!(left, right);
assert_ne!(left, right);
assert!(condition, "message {}", value);

assert! for Boolean Conditions

assert!(condition) panics with a generic message if the given boolean expression evaluates to false, otherwise it does nothing.

Example: assert! for Boolean Conditions

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `value`, set to `10`
    let value = 10;
    // Assert that value > 0 — panics if this is false
    assert!(value > 0);
    // Print "Assertion passed: value is positive" to the console (with a trailing newline)
    println!("Assertion passed: value is positive");
}

assert_eq! for Equality Checks

assert_eq!(left, right) panics if the two values are not equal, and importantly prints both values in the panic message, which makes debugging test failures much easier than a plain assert!.

Example: assert_eq! for Equality Checks

markup
// Define the function `square` taking `n` and returning an integer
fn square(n: i32) -> i32 {
    n * n
}

// Entry point: execution of the program starts here
fn main() {
    // Assert that square(4), 16 — panics if this is false
    assert_eq!(square(4), 16);
    // Print "Assertion passed: square(4) == 16" to the console (with a trailing newline)
    println!("Assertion passed: square(4) == 16");
}

assert_ne! for Inequality Checks

assert_ne!(left, right) is the opposite of assert_eq!, panicking if the two values turn out to be equal when they were expected to differ.

Example: assert_ne! for Inequality Checks

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `original`, set to `5`
    let original = 5;
    // Declare an immutable variable `modified`, set to `original + 1`
    let modified = original + 1;
    assert_ne!(original, modified);
    // Print "Assertion passed: values are different" to the console (with a trailing newline)
    println!("Assertion passed: values are different");
}

Adding a Custom Panic Message

All three assert macros accept an optional format string and arguments after the main condition, producing a clearer, more specific panic message on failure.

Example: Adding a Custom Panic Message

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `count`, set to `3`
    let count = 3;
    // Assert that count > 0, "count must be positive, got {}", count — panics if this is false
    assert!(count > 0, "count must be positive, got {}", count);
    // Print "Assertion passed with count = {}" to the console, substituting in `count` (with a trailing newline)
    println!("Assertion passed with count = {}", count);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using assert_eq! with values that don't implement Debug, since the macro needs Debug to print both sides on failure.
  2. Confusing assert! (checks a single boolean) with assert_eq!/assert_ne! (compares two values).
  3. Forgetting assert macros work outside of tests too, and will panic the whole program if their condition fails at runtime, not just during cargo test.
Chapter Summary
  • assert!(condition) panics if the condition is false.
  • assert_eq!(a, b) panics if a and b are not equal, printing both values for easy debugging.
  • assert_ne!(a, b) panics if a and b are equal.
  • All three macros work anywhere in a program, not just inside #[test] functions, and accept an optional custom panic message.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.