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:
- The
letkeyword is used to declare a variable in Rust. - The
assert_eq!macro is used to assert that two values are equal and throw an error if they are not. - The
assert_ne!macro is used to assert that two values are not equal and throw an error if they are. - The
panic!macro is used to throw an error when an assertion fails.
Helpful links:
More of Rust
- How to use non-capturing groups in Rust regex?
- How to use regex lookbehind in Rust?
- How to use regex lookahead in Rust?
- How to match whitespace with a regex in Rust?
- How to perform matrix operations in Rust?
- How to convert a u8 slice to a hex string in Rust?
- How to sort a Rust HashMap?
- How to replace strings using Rust regex?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to make regex case insensitive in Rust?
See more codes...