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&strargument: the error message
Helpful links
More of Rust
- How to replace a capture group 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?
- Weak pointer example in Rust
- How do I declare a variable without initializing it in Rust?
- How to match whitespace with a regex in Rust?
- How to convert a Rust HashMap to JSON?
- How to use a custom hash function with a Rust HashMap?
- How to match a URL with a regex in Rust?
See more codes...