An assertion checks a condition your program expects to hold. If the check fails, Rust panics, making a logic bug visible instead of letting the program continue unnoticed. section 6.3
Rust provides three assertion macros:
- assert! checks that a condition is true. section 6.3
- assert_eq! checks that two values are equal; assert_ne! checks that they are not equal. section 6.3
- Each macro can take a custom message, displayed if the assertion fails. section 6.3
fn main() {
let my_name = "Loki Laufeyson";
assert!(my_name == "Loki Laufeyson");
assert_eq!(my_name, "Loki Laufeyson");
assert_ne!(my_name, "Mithridates");
}
These checks pass because the name matches the expected value and differs from Mithridates. section 6.3














