Error Handling
Unrecoverable and Recoverable
Rust has both unrecoverable (array index out of bound ) and recoverable (file not found)
Abort vs Panic
When panic!
is used, rust cleans up from bottom to top where the error started to occur from however abort!
just aborts the program without cleaning. The cleaning up of memory will be done by Operating system in this case
? Operator
Use this operator if you are returning the Result<T,E> from any function. For instance look at the example below
fn open_file_with() -> Result<String, Error> {
let mut f = File::open("hello.text")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
As you could see above the function is returning Result<T,E> type
Last updated
Was this helpful?