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 variablex
and assigns it the value5
.assert!(x == 5);
: This statement uses theassert!
macro to check if the expressionx == 5
evaluates to true. If it does, the macro does nothing, otherwise it will panic.
Helpful links:
More of Rust
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...