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
- How to replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace all matches using Rust regex?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- Regex example to match multiline string in Rust?
- Bitwise XOR operator usage in Rust
- How to convert a Rust HashMap to a BTreeMap?
- How to match digits with regex in Rust?
- How to reverse a Rust slice?
See more codes...