rustHow to throw an exception in Rust
Throwing an exception in Rust is done using the panic!
macro. This macro will cause the program to immediately exit with a message.
Example code
fn main() {
panic!("Something went wrong!");
}
Output example
thread 'main' panicked at 'Something went wrong!', src/main.rs:2:4
Code explanation
panic!
: The macro used to throw an exception.Something went wrong!
: The message that will be printed when the exception is thrown.
Helpful links
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...