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
let
keyword 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 get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to match the end of a line in a Rust regex?
- Hashshet example in Rust
- How to create a new Rust HashMap with values?
- How do I use a variable from another file in Rust?
- How to get the length of a Rust HashMap?
- How to get an element from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...