rustHow to throw custom error in Rust
Rust provides the panic!
macro to throw custom errors. It takes a &str
argument which is the error message.
Example code
fn main() {
panic!("Custom error message");
}
Output example
thread 'main' panicked at 'Custom error message', src/main.rs:2:5
The code consists of:
panic!
macro: used to throw a custom error&str
argument: the error message
Helpful links
More of Rust
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to use non-capturing groups in Rust regex?
- How to convert a Rust HashMap to a BTreeMap?
- How to implement a generator trait in Rust?
- Bitwise operator example in Rust
- How to set default value in Rust struct
- How to match whitespace with a regex in Rust?
See more codes...