1 00:00:06,070 --> 00:00:12,430 In this lecture we will begin using the reg x crate we put in our dependencies earlier on. 2 00:00:12,760 --> 00:00:16,210 So let's start out by just simply importing it in. 3 00:00:20,600 --> 00:00:24,710 And now we want to create a new function called replace. 4 00:00:25,400 --> 00:00:29,070 And it's going to accept three parameters. 5 00:00:29,090 --> 00:00:33,530 Our first one is going to be our target, which is going to be a reference to a string slice. 6 00:00:34,330 --> 00:00:40,480 And our target is going to be the pattern that we want to replace. 7 00:00:41,080 --> 00:00:45,310 And then we're going to have our replace, which is also a string slice. 8 00:00:45,310 --> 00:00:48,760 And that's going to be what we replace our target with. 9 00:00:50,320 --> 00:00:59,260 And then we have our data, which is also a reference to a string slice, and our data is going to reference 10 00:00:59,260 --> 00:01:05,800 the input, the text we read in from our input file. 11 00:01:06,370 --> 00:01:09,280 So now we're going to return a result. 12 00:01:09,280 --> 00:01:14,280 And in the event that the result is OC, we want to return it with a string. 13 00:01:14,290 --> 00:01:18,250 Otherwise, if it's an air, we want to return a reg x air. 14 00:01:19,910 --> 00:01:22,220 So now we're going to call regex new. 15 00:01:22,340 --> 00:01:26,900 So we'll say let reg x equals reg x new. 16 00:01:27,140 --> 00:01:34,700 And it's going to be done on our target, which is the string that we want to look for and this method 17 00:01:34,700 --> 00:01:35,540 can fail. 18 00:01:35,540 --> 00:01:41,300 So in the event that it does fail, we want to propagate it up, which we learned earlier on in this 19 00:01:41,300 --> 00:01:44,180 course that that is done by. 20 00:01:45,290 --> 00:01:46,970 Adding a question mark at the end. 21 00:01:48,560 --> 00:01:50,690 So assuming that the method. 22 00:01:52,090 --> 00:01:54,550 Successfully runs, then we want to return. 23 00:01:54,550 --> 00:01:55,200 Okay. 24 00:01:55,210 --> 00:01:59,350 And then we want to say reg x dot replace all. 25 00:02:00,850 --> 00:02:11,350 Replace all, and then we want to replace all of the targeted strings inside of data with the string 26 00:02:11,350 --> 00:02:13,030 that we want it to be replaced with. 27 00:02:14,280 --> 00:02:20,670 And then we want to just say to string because that is how our result expects it to be returned. 28 00:02:20,670 --> 00:02:27,990 So we are making sure it is explicitly going to be returned as a string and that's all we have to do 29 00:02:27,990 --> 00:02:30,480 inside of that function. 30 00:02:30,480 --> 00:02:33,330 So now we need to add this into our core logic. 31 00:02:33,330 --> 00:02:39,870 So we're going to say let replace data and then we're going to match on replace. 32 00:02:41,490 --> 00:02:45,150 And our target is our dot pattern. 33 00:02:46,320 --> 00:02:50,850 Our replacement string is our replace. 34 00:02:51,180 --> 00:02:57,810 And then our data is just a reference to the data we created right here. 35 00:03:00,150 --> 00:03:01,800 And then inside of here. 36 00:03:01,830 --> 00:03:02,140 Oop. 37 00:03:02,220 --> 00:03:04,470 I need to make this as a reference. 38 00:03:04,980 --> 00:03:13,110 And then inside of here, if it is okay, then we want that data to be given to us and assign to replace 39 00:03:13,110 --> 00:03:13,710 data. 40 00:03:14,130 --> 00:03:20,940 And then otherwise, if it's an air, well, then we're going to just kind of follow what we have been 41 00:03:20,940 --> 00:03:21,330 doing. 42 00:03:21,330 --> 00:03:29,970 So our first thing is going to be the red and bold error word, and then we're going to say fail to 43 00:03:29,970 --> 00:03:34,890 replace text and then we want our error message to be printed out. 44 00:03:35,400 --> 00:03:43,800 So now we're going to say air dot red, dot bold, and now our error message. 45 00:03:44,640 --> 00:03:50,850 And then we want our process to exit with error code one. 46 00:03:53,840 --> 00:03:58,310 And now for writing to the new file we want to write in our. 47 00:03:59,440 --> 00:04:01,180 Replaced data. 48 00:04:01,980 --> 00:04:04,170 And that should be all we have to do. 49 00:04:04,200 --> 00:04:06,720 So now let's test this out. 50 00:04:06,720 --> 00:04:11,790 So we're going to create a new file called Test Dot Text. 51 00:04:11,790 --> 00:04:16,650 And then inside of here, we will say this is a test. 52 00:04:18,120 --> 00:04:18,990 We'll save it. 53 00:04:19,770 --> 00:04:22,950 And now we can run our program, say cargo run. 54 00:04:22,950 --> 00:04:30,330 And we will say this is a test and that's what we want to replace and we want to replace it with 55 00:04:32,550 --> 00:04:33,450 success. 56 00:04:34,260 --> 00:04:41,280 And then our input file is test txt and then test modify. 57 00:04:41,850 --> 00:04:43,560 It will be our output file. 58 00:04:43,740 --> 00:04:48,890 So now let's run it and it says wrong number of arguments given. 59 00:04:48,900 --> 00:04:57,490 Oh, I need to put these inside of quotations. 60 00:04:57,510 --> 00:04:58,200 There we go. 61 00:04:59,720 --> 00:05:05,650 So now let's check our test mod, our test mod, and we expect it to just say success. 62 00:05:05,660 --> 00:05:09,260 And inside that is exactly what it says. 63 00:05:09,260 --> 00:05:15,140 So we have successfully found and replaced strings. 64 00:05:16,040 --> 00:05:19,730 But let's make sure that our air handling is correct as well. 65 00:05:20,030 --> 00:05:20,840 So. 66 00:05:22,000 --> 00:05:32,500 We'll look for an open and closed bracket and we'll just say inside of will replace it with test and 67 00:05:32,500 --> 00:05:36,990 then we'll say test test and we'll say test. 68 00:05:37,030 --> 00:05:44,140 So I know that this should fail, but let's make sure that it does and it does. 69 00:05:44,140 --> 00:05:48,880 So we see that we have a reg x pass ere an enclosed character class. 70 00:05:49,790 --> 00:05:52,150 And it failed right here. 71 00:05:52,160 --> 00:05:56,090 So we see that our air was propagated up. 72 00:05:56,300 --> 00:06:04,790 So now we have successfully handled our errors and created a find and replace command line program. 73 00:06:05,270 --> 00:06:05,960 So. 74 00:06:07,080 --> 00:06:14,340 We have successfully put together a lot of the main points we have learned about so far in this course. 75 00:06:14,550 --> 00:06:22,020 So congratulations on the achievement of making it this far and understanding the logic behind what 76 00:06:22,020 --> 00:06:23,130 we have been doing. 77 00:06:23,160 --> 00:06:27,720 So let's continue our rest journey in the next section.