handling Method in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Rust 1.55

In Rust 1.55, you can use the try, catch, and finally keywords to handle errors in a method.

The try keyword is used to mark an expression that may result in an error. If an error occurs, the catch block will handle the error. The finally block is used to run code after the try and catch blocks have run, regardless of whether an error occurred.

Here is an example method that uses try, catch, and finally:

use std::fs::File;
use std::io::{ErrorKind, Read};

fn read_file_contents(file_path: &str) -> Result {
let mut file = match File::open(file_path) {
Ok(file) => file,
Err(error) => {
let error_message = match error.kind() {
ErrorKind::NotFound => format!("File not found: {}", file_path),
_ => format!("Error opening file: {}", error),
};
return Err(error_message);
}
};

let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => Ok(contents),
Err(error) => Err(format!("Error reading file: {}", error)),
}
}

In this method, we use match to handle errors that may occur when opening the file and when reading the contents of the file. If an error occurs, Err is returned with an error message.

Now, let's add try, catch, and finally to this method:

use std::fs::File;
use std::io::{ErrorKind, Read};

fn read_file_contents_try(file_path: &str) -> Result {
let mut file = try!(File::open(file_path).map_err(|error| {
let error_message = match error.kind() {
ErrorKind::NotFound => format!("File not found: {}", file_path),
_ => format!("Error opening file: {}", error),
};
error_message
}));

let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => Ok(contents),
Err(error) => Err(format!("Error reading file: {}", error)),
}
.map_err(|error| format!("Error: {}", error))
.finally(|| println!("Finished reading file"));
}

In this version of the method, we use try! to handle errors that may occur when opening the file. If an error occurs, the catch block is executed and an error message is returned. If no error occurs, the try block is executed and the contents of the file are read.

After the try and catch blocks, we use .map_err to map any read errors to an error message. Finally, we use .finally to print a message after the try and catch blocks have run, regardless of whether an error occurred.

Here's an example of how to use the read_file_contents_try method:

fn main() {
    let file_contents = read_file_contents_try("test.txt");

match file_contents {
Ok(contents) => println!("{}", contents),
Err(error) => println!("Error: {}", error),
}
}

This code will try to open and read the contents of the file test.txt. If no errors occur, the contents of the file will be printed to the console. If an error occurs, an error message will be printed to the console instead. Regardless of whether an error occurred, the message "Finished reading file" will be printed to the console.