Error-handling in Rust

Irrespective of the programming language, errors will occur. We need to be able to handle these errors. In Rust programming, we have two types of errors:

  1. Unrecoverable errors: Like Macro in Clang, Rust has a similar panic! function. Take a look at the following example, which shows how we can handle unrecoverable errors using the panic! function inside the main:
fn main() 
{
panic!("Something is wrong... Check for Errors");
}
  1. Recoverable error: This is a standard part of programming and is nicely handled by Rust. Take a look at the following example. When you call File::open and an error occurs, it will call the panic function:
use std::fs::File;
fn main() {
let _E = File::open("PacktDocument.txt");
let _E = match _E
{ Ok(file) => file,
Err(why) => panic!("Something is wrong with the Document {:?}", why),
};
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.108.54