From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Matching Result<T, E> to recover from errors

Matching Result<T, E> to recover from errors - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Matching Result<T, E> to recover from errors

- [Instructor] Rather than simply panicking if a result enum is an error by using the unwrap or expect methods to extract it stored value, a better solution is to use a match expression to process the result. And in the case that it is an error, you can potentially take some other action to resolve it. Continuing from the instate of the previous video, let's delete the expect method from line four. And since we're no longer extracting the contents from results with the expect method, let's also change the variable name from contents to result to better reflect that it's a result enum. Next, we'll use a match expression to get the contents from the resulte enum. Let contents = match result. If the enum is okay, then it will be storing the message that was read from a file. So let's return that message string. And if the result enum was an error, rather than panic and terminate the program like before, let's return…

Contents