Module lib::assert

source ·
Expand description

Assert a condition is true.

  • If true, return ().

  • Otherwise, call panic! with a message and the values of the expressions with their debug representations.

§Examples

// Return Ok
assert!(true);
//-> ()

// Panic with error message
let result = panic::catch_unwind(|| {
assert!(false);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
    "assertion failed: false"
);
assert_eq!(actual, expect);

The assert macro is a Rust built-in.