Skip to main content

Assertions in Rust

Assertions in Rust

Assertions are fundamental in Rust testing and debugging. They ensure that conditions hold true during execution, helping to identify logic errors early. Rust provides various assertion macros to validate conditions, making it easier to write reliable code.


1. Understanding Assertions

Assertions verify that a given condition is met. If the condition fails, the program panics and terminates. This is useful for enforcing invariants in code, preventing unintended behavior.


2. Using assert! Macro

The assert! macro checks if an expression evaluates to true. If it evaluates to false, the program panics.

<!-- Basic assert! example -->
fn main() {
    let value = 10;
    assert!(value > 5, "Value must be greater than 5");
}

Output when assertion fails:

thread 'main' panicked at 'Value must be greater than 5', src/main.rs:3:5

3. Using assert_eq! for Equality Checks

The assert_eq! macro verifies that two values are equal. If they are not, the program panics and shows both values for debugging.

<!-- Using assert_eq! -->
fn main() {
    let a = 10;
    let b = 2 * 5;
    assert_eq!(a, b, "a and b must be equal");
}

Output when assertion fails:

thread 'main' panicked at 'a and b must be equal', src/main.rs:4:5
assertion failed: (left == right)
left: `10`,
right: `15`

4. Using assert_ne! for Inequality Checks

The assert_ne! macro checks that two values are not equal. If they are equal, the program panics.

<!-- Using assert_ne! -->
fn main() {
    let x = 5;
    let y = 10;
    assert_ne!(x, y, "x and y should not be equal");
}

Output when assertion fails:

thread 'main' panicked at 'x and y should not be equal', src/main.rs:4:5
assertion failed: (left != right)
left: `10`,
right: `10`

5. Custom Error Messages

All assertion macros accept an optional error message, which helps in debugging.

<!-- Assertion with custom message -->
fn main() {
    let count = 42;
    assert!(count % 2 == 0, "Count should be an even number, got {}", count);
}

6. Assertions in Tests

Assertions are widely used in Rust unit tests. They validate function correctness and catch regressions.

<!-- Using assertions in tests -->
#[cfg(test)]
mod tests {
    #[test]
    fn test_addition() {
        let result = 2 + 2;
        assert_eq!(result, 4, "Addition failed");
    }
}

Run tests using:

cargo test

7. Debug Assertions

Rust provides debug-only assertions using debug_assert!, debug_assert_eq!, and debug_assert_ne!. These assertions are only active in debug mode and ignored in release builds.

<!-- Using debug_assert! -->
fn main() {
    let speed = 100;
    debug_assert!(speed < 120, "Speed should not exceed 120 km/h");
}

To enable debug assertions, compile without optimizations:

cargo run

In release mode, debug assertions are ignored:

cargo run --release

8. Conclusion

  • Assertions help catch logic errors early by enforcing conditions.
  • assert! ensures a boolean condition is true.
  • assert_eq! checks for equality, while assert_ne! verifies inequality.
  • Debug assertions (debug_assert!) run only in debug mode.
  • Assertions are essential for writing robust tests in Rust.

By leveraging assertions effectively, developers can write safer, more maintainable Rust code.

Comments