rustHow to assert errors in Rust
Rust provides the assert! macro to assert errors. This macro takes a boolean expression as an argument and will panic if the expression evaluates to false.
Code example:
let x = 5;
assert!(x == 5);
Output
No output, as the expression evaluates to true.
Explanation:
let x = 5;: This statement declares a variablexand assigns it the value5.assert!(x == 5);: This statement uses theassert!macro to check if the expressionx == 5evaluates to true. If it does, the macro does nothing, otherwise it will panic.
Helpful links:
More of Rust
- How to use binary regex in Rust?
- How to map a Rust slice?
- How to compare two Rust HashMaps?
- How to yield a thread in Rust?
- How to make regex case insensitive in Rust?
- How to use regex to match a group in Rust?
- How to match whitespace with a regex in Rust?
- How to use regex lookbehind in Rust?
- How to match digits with regex in Rust?
- How to use regex to match a double quote in Rust?
See more codes...