#rust
- Rust groups errors into two categories: recoverable and unrecoverable errors.
- Rust provides
Result<T, E>
for recoverable errors and thepanic!
macro to stop execution for unrecoverable errors.
- There are two ways to cause a panic in practice: by taking an action that causes our code to panic (such as accessing an array past the end) or by explicitly calling the
panic!
macro.
fn main() {
panic!("crash and burn");
}
- By default, these panics will print a failure message, unwind, clean up the stack, and quit. However, unwinding is a lot of work. Therefore, Rust allows you to choose an alternative of immediately aborting, which means that the memory used by the program will then need to be cleaned up by the OS.
# Cargo.toml
[profile.release]
panic = 'abort'
- Panic backtraces can be obtained by setting the environment variable
RUST_BACKTRACE
to anything except 0.
- When choosing between whether to
panic!
or returnResult
, the latter is a good default choice for a function that might fail because this gives the calling code an opportunity to recover. - However, examples, prototype code, and tests are situations in which it is better to panic.
- Guidelines for Error Handling
- As a programmer, one could also create custom types to validation. An example of a custom type to ensure an input number is between 1..100 is shown here.
pub struct Guess {
value: i32,
}
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 || value > 100 {
panic!("Value should be between 1 and 100");
}
Guess { value }
}
pub fn value(&self) -> i32 {
self.value
}
}