9951 explained code solutions for 126 technologies


rustHow to assert error type in Rust


In Rust, you can use the assert_eq! macro to assert that two values are equal and throw an error if they are not. You can also use the assert_ne! macro to assert that two values are not equal and throw an error if they are.

Code example:

let x = 5;
let y = 10;

assert_eq!(x, y);

Output

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `5`,
 right: `10`', src/main.rs:3:5

Explanation:

  1. The let keyword is used to declare a variable in Rust.
  2. The assert_eq! macro is used to assert that two values are equal and throw an error if they are not.
  3. The assert_ne! macro is used to assert that two values are not equal and throw an error if they are.
  4. The panic! macro is used to throw an error when an assertion fails.

Helpful links:

  1. Rust Documentation - Macros
  2. Rust Documentation - Error Handling

Edit this code on GitHub