>> Let's get some hands-on
experience with error handling and Rust.
I'll start by creating a new project
called errors in my terminal now.
Then I'll open up this project in VS Code.
I'll open up the main.rs file,
and start by looking at an example with the panic macro.
Inside of the main function,
I'm going to go ahead and get rid of this println line,
and use the panic macro.
Inside of that, I will have the text farewell,
then go ahead and close that with a semicolon.
I can run the program using cargo run.
Then I'll see that the program
panicked at line two with the message farewell.
Let's look at another example that
doesn't explicitly throw a panic,
but will ultimately result in an unrecoverable state.
What I want to do on line two is remove this panic statement.
I'm going to then specify a vector
named v. This vector is going to have the value zero,
one, two, and three.
On the next line,
I will use a print statement to go
ahead and print out an index of the vector.
Let's try to print out the index of six.
Let's go ahead and see what happens when we do this now.
After running this program,
I see that the program panicked because of an index out of bounds.
Panics can be thrown in programs when
they reach an unrecoverable state,
like you see here.
Whether or not we use the panic keywords,
sometimes panics will arise in your programs.
Let's look at an example where we use the option enum,
where absence of a value is a possibility,
and can either return some or none asset value.
I am going to go ahead and comment these lines
out here for announcements they are throwing a panic.
What I'm going to do is to find a new variable called fruits.
This is going to be a vector of different fruits.
Let's go ahead and put some fruits in our vector.
We're going to have a banana,
an apple, and a coconut.
Then I'm going to close that with a semicolon.
What I want to do with this vector is to access fruits.
I want to say let first is equal to fruit.get zero.
I just want to go ahead and grab the item and not zero index.
Then I can say println.
I want to grab the first variable and print that out there.
Then let's say that I have a variable called third,
and I want to go ahead and get
that third item in the fruits vector.
For this I can say fruits.get two.
Then similarly, I'll go ahead and print that out,
like this, and add a semicolon at the end.
Then for one more example here,
I want to try to access a vector's non-existent index.
Let's go ahead and say let non_existent is equal to fruits.get.
We can just throw any index
that we want in here but something that's
obviously just not going to be existent in our vector.
Then on the next line I'll go ahead and print out that value.
Let me go ahead and run this.
I see printed out some banana,
some coconut, and then finally none.
Since a value existed is specify index,
like it does with the first and the third items,
it's going to print out the actual value.
It's using that option, some value variant.
If the index is out of balance,
like it is with a nonexistent variable,
it's going to access the option non-value variant and return none.
This is a good example here of seeing this option enum in action.
I'm going to look at one more example of using the result enum.
I will first go ahead and comment out this example here.
Within the result enum,
we'll return okay if there's a value,
or error if there's an error.
Outside of the main function at the top of the file,
I am going to import the standard library fs and use file.
Then inside of the main function,
after the commented code,
I want you to find a new variable called let
f is going to be equal to file open.
Then I'm going to put the name of the file within the quotes here.
After that, I want to say let f is equal to match f. Then
I'll use the result enum to try to unwrap the file contents.
I'm going to say okay, file,
and then return the file if in vector is a file found.
Otherwise, this will result in an error.
Then the program will panic.
I can say that we can't open the file.
Let's print out that error after that.
Then I'll go ahead and add a semicolon at the end of this.
Let's try to run the program as
is to go ahead and see the output now.
After running the program,
I see that read mean panicked,
can't open the file,
and the message is no such file or directory.
I also know that this error is coming from line 22.
This is just one way of how you can use this match statement,
where I either return an okay or an error.
Something else that I want to go ahead and do though,
is show examples for how we can use
the unwrap and expect helper methods.
I can go ahead and get rid of this match statement here.
Let's go ahead and try to use
the unwrap helper method just like that,
and see what happens now when we run the program.
This also results in a panic.
At line 18, since there is no such file or directory,
this is much less code needed for
essentially the same result output that we get here.
Another way that we can handle
this situation is to use the expect helper method.
Instead of using unwrap,
I can go ahead and call expect here.
What's different about expect than unwrap,
is that I can go ahead and add
a message here to provide
a little bit more contacts in case of a panic.
I can say failed to open hello.txt.
Let's go ahead and run the program again.
Notice that this error is a little bit different.
Here I have this message,
failed to open hello text that I defined.
I also didn't have no such file or directory.
Really the convenient thing here is being able to
specify an extra message that makes it a little bit more clearer,
if this were to panic where it's happening in the program.
It can definitely save you some time debugging.
When integrating error handling in your programs,
consider which of these options
makes the most sense for your use case.
As you become more familiar with Rust,
I recommend trying out multiple ways of error handling,
to decide what works best in different scenarios.
[MUSIC]