How to assert in Rust?
To assert in rust we use functionassert!()
this function takes first parameters as condition, second parameter is optional and it will be a message in case of failuer.fn main() {
// Panics
assert!(1 == 2);
}
Output
thread 'main' panicked at 'assertion failed: 1 == 2', src/main.rs:3:5
Example of rust assert with failure message
fn main() {
// Panics with a message
assert!(1 == 2, "Numbers are not equal");
}
Output
thread 'main' panicked at 'Numbers are not equal', src/main.rs:3:5