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 match a URL with a regex in Rust?
- How to replace strings using Rust regex?
- How to get a capture group using Rust regex?
- How to extract data with regex in Rust?
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to perform matrix operations in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to parse JSON string in Rust?
- How to get all values from a Rust HashMap?
See more codes...