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 match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to split a string with Rust regex?
- How to iterate over a Rust slice with an index?
- How to use negation in Rust regex?
- How to use regex captures in Rust?
- Regex example to match multiline string in Rust?
- How to get a capture group using Rust regex?
- How to use modifiers in a Rust regex?
- How to create a HashMap of structs in Rust?
See more codes...