rustHow to throw error in Rust
Rust provides the panic!
macro to throw an error. This macro will cause the program to immediately exit with a message.
Code example:
fn main() {
panic!("This is an error message");
}
Output
thread 'main' panicked at 'This is an error message', src/main.rs:2:4
Explanation:
panic!
is a macro provided by Rust that will cause the program to immediately exit with a message.- The message is provided as an argument to the macro.
- In the example above, the message is
This is an error message
. - The output shows that the panic occurred on line 2 of the main.rs file.
Helpful links:
More of Rust
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to match the end of a line in a Rust regex?
- Hashshet example in Rust
- How to create a new Rust HashMap with values?
- How do I use a variable from another file in Rust?
- How to get the length of a Rust HashMap?
- How to get an element from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...