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 regex to match a double quote in Rust?
- How to create a HashMap of structs in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to modify an existing entry in a Rust HashMap?
- How do I identify unused variables in Rust?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use a Rust HashMap in a struct?
- How do I use a variable number of arguments in Rust?
See more codes...