So as you can imagine, if we always try to catch and handle the air immediately, that's going to be
a lot of match statements as we saw up here in this example.
In this case, we can propagate the air up the call stack and we can use this by using a question mark.
Operator And we can use a question mark to any expression that produces a result.
On success, it unwraps the result to get the success, the success value inside, but on air it immediately
returns from the enclosing function and passes the error result up the call chain.
So let's look at how we can test this.
So there's a couple of items we need to bring into scope.
So we're going to use the file system and then rename and then we're going to use standard IO and then
air.
So inside of.
Maine, we're going to actually call a function called let test equal open file.
And now we want to create a function down here called Open File.
And it's going to return a result of either a file or an error.
And now inside here we can say let file equal.
File open air dot txt and then on oc.
We just want to send back the file.
And I want to put a question mark here, because that's the whole purpose of this.
So this question mark is how we can propagate the air up.
So now if we run cargo run, everything looks good because we didn't use the test variables.
So we'll say test, dot, unwrap.
And now if we run it, we see that our air was propagated up.
So it says result unwrap on air value.
So our air was indeed propagated up.
But it.
If we make this correct and run it, everything runs cleanly.
So it's just a much easier way of handling these affairs without having an enormous amount of.
Match statements to kind of go along with them.
So another example that we can look at is renaming a file because renaming a file can fail as well.
So we're going to have to return a result and we'll just give it an empty generic and.
The air that can be returned.
So we'll say let file equals
rename because we brought that into scope and we'll say we'll rename from air dot txt.
To renamed txt and then we will return the OC there and we need to put our question mark there.
So now we'll say here we actually don't even have to do this.
We'll say.
Rename file.
Dot.
Unwrap.
And now that we know that we have an air created, we expect air test to be renamed to rename the text
file.
And as you can see, that is what happens.
So we cannot typically use the question mark.
Operator in the main method, because there is not anything for it to be passed up to.
So in most cases, you would want to use expect in the main thread.
But all in all, this is really all there is to air handling, aside from being able to generate our
own custom errors, which we'll do later on in this course.
But for now, that is practically all there is to it for general air handling.