1 00:00:05,880 --> 00:00:11,430 So as you can imagine, if we always try to catch and handle the air immediately, that's going to be 2 00:00:11,430 --> 00:00:16,020 a lot of match statements as we saw up here in this example. 3 00:00:17,660 --> 00:00:26,870 In this case, we can propagate the air up the call stack and we can use this by using a question mark. 4 00:00:26,900 --> 00:00:32,840 Operator And we can use a question mark to any expression that produces a result. 5 00:00:32,870 --> 00:00:41,870 On success, it unwraps the result to get the success, the success value inside, but on air it immediately 6 00:00:41,870 --> 00:00:46,470 returns from the enclosing function and passes the error result up the call chain. 7 00:00:46,490 --> 00:00:48,470 So let's look at how we can test this. 8 00:00:49,290 --> 00:00:52,680 So there's a couple of items we need to bring into scope. 9 00:00:52,680 --> 00:01:03,900 So we're going to use the file system and then rename and then we're going to use standard IO and then 10 00:01:03,900 --> 00:01:04,530 air. 11 00:01:07,660 --> 00:01:09,850 So inside of. 12 00:01:11,220 --> 00:01:22,230 Maine, we're going to actually call a function called let test equal open file. 13 00:01:22,740 --> 00:01:28,980 And now we want to create a function down here called Open File. 14 00:01:30,570 --> 00:01:36,930 And it's going to return a result of either a file or an error. 15 00:01:39,320 --> 00:01:44,090 And now inside here we can say let file equal. 16 00:01:46,540 --> 00:01:55,510 File open air dot txt and then on oc. 17 00:01:55,540 --> 00:01:58,180 We just want to send back the file. 18 00:01:59,640 --> 00:02:03,780 And I want to put a question mark here, because that's the whole purpose of this. 19 00:02:03,780 --> 00:02:08,340 So this question mark is how we can propagate the air up. 20 00:02:09,710 --> 00:02:17,270 So now if we run cargo run, everything looks good because we didn't use the test variables. 21 00:02:17,270 --> 00:02:19,850 So we'll say test, dot, unwrap. 22 00:02:20,780 --> 00:02:25,100 And now if we run it, we see that our air was propagated up. 23 00:02:25,100 --> 00:02:28,040 So it says result unwrap on air value. 24 00:02:28,040 --> 00:02:31,610 So our air was indeed propagated up. 25 00:02:31,610 --> 00:02:32,160 But it. 26 00:02:33,170 --> 00:02:40,790 If we make this correct and run it, everything runs cleanly. 27 00:02:41,780 --> 00:02:51,950 So it's just a much easier way of handling these affairs without having an enormous amount of. 28 00:02:53,850 --> 00:02:56,430 Match statements to kind of go along with them. 29 00:02:57,060 --> 00:03:03,930 So another example that we can look at is renaming a file because renaming a file can fail as well. 30 00:03:05,670 --> 00:03:13,830 So we're going to have to return a result and we'll just give it an empty generic and. 31 00:03:16,390 --> 00:03:17,870 The air that can be returned. 32 00:03:17,880 --> 00:03:21,150 So we'll say let file equals 33 00:03:23,430 --> 00:03:30,570 rename because we brought that into scope and we'll say we'll rename from air dot txt. 34 00:03:34,300 --> 00:03:46,750 To renamed txt and then we will return the OC there and we need to put our question mark there. 35 00:03:47,020 --> 00:03:51,190 So now we'll say here we actually don't even have to do this. 36 00:03:52,780 --> 00:03:53,800 We'll say. 37 00:03:55,930 --> 00:03:57,280 Rename file. 38 00:04:00,460 --> 00:04:03,250 Rename file. 39 00:04:04,060 --> 00:04:04,720 Dot. 40 00:04:05,350 --> 00:04:06,190 Unwrap. 41 00:04:07,410 --> 00:04:16,560 And now that we know that we have an air created, we expect air test to be renamed to rename the text 42 00:04:16,560 --> 00:04:17,160 file. 43 00:04:18,030 --> 00:04:20,910 And as you can see, that is what happens. 44 00:04:21,390 --> 00:04:26,730 So we cannot typically use the question mark. 45 00:04:26,730 --> 00:04:33,540 Operator in the main method, because there is not anything for it to be passed up to. 46 00:04:33,570 --> 00:04:38,700 So in most cases, you would want to use expect in the main thread. 47 00:04:39,600 --> 00:04:46,770 But all in all, this is really all there is to air handling, aside from being able to generate our 48 00:04:46,770 --> 00:04:51,900 own custom errors, which we'll do later on in this course. 49 00:04:52,020 --> 00:04:56,700 But for now, that is practically all there is to it for general air handling.