1 00:00:05,920 --> 00:00:09,280 The first thing we need to do is obviously just create a new project. 2 00:00:09,280 --> 00:00:10,570 So I went ahead and did that. 3 00:00:10,570 --> 00:00:15,820 I named mine Clive for command line interface, but feel free to name yours whatever you want. 4 00:00:16,210 --> 00:00:22,630 So the next thing we're going to do is add in a couple of dependencies into our cargo Tamil file. 5 00:00:22,630 --> 00:00:25,840 And the first one is going to be called text color riser. 6 00:00:26,120 --> 00:00:31,450 We'll set the version to one and then rig X and we'll also set the version to one. 7 00:00:31,540 --> 00:00:34,360 So just to kind of show you what these dependencies are. 8 00:00:34,570 --> 00:00:40,840 Text Polarizer, it's just going to give us a couple of functions to enable and disable coloring. 9 00:00:40,840 --> 00:00:47,110 And this is just going to allow us to add some style to our output in the terminal. 10 00:00:48,030 --> 00:00:54,000 And then we have our Reg X crate, which is short for regular expression, and this creates going to 11 00:00:54,000 --> 00:01:03,060 allow us to look for a certain expression in our file and then we'll be able to. 12 00:01:03,940 --> 00:01:08,940 Change it based on some arguments that we're going to accept in. 13 00:01:08,950 --> 00:01:13,270 So this is a very important crate for what our program is going to do. 14 00:01:14,540 --> 00:01:15,230 So. 15 00:01:16,230 --> 00:01:17,450 Just to come back and look in here. 16 00:01:17,450 --> 00:01:21,020 We see we have reg x equals to one. 17 00:01:23,930 --> 00:01:29,760 Argo does is really cool thing where crate set of reached a 1.0 version in rust. 18 00:01:29,780 --> 00:01:32,540 They begin to follow semantic versioning. 19 00:01:32,840 --> 00:01:38,590 This means that all version one changes should maintain compatibility with older versions. 20 00:01:38,600 --> 00:01:43,850 So version 1.2 will be compatible with 1.3 and so on. 21 00:01:44,510 --> 00:01:49,130 But once version 2.0 is released, then 2.0 is. 22 00:01:50,070 --> 00:01:54,180 No longer required to be compatible with 1.0 versions. 23 00:01:54,510 --> 00:01:58,650 So another thing is, if you saw this in the Reg X. 24 00:01:59,560 --> 00:02:07,060 We're at version 1.5.4 is the latest, but in here we have it only as version one. 25 00:02:08,170 --> 00:02:11,920 So cargo also does this thing where if we specify, hey, we just want. 26 00:02:12,980 --> 00:02:20,800 One for the version is going to automatically use the newest available version prior to version two. 27 00:02:20,810 --> 00:02:26,060 So this one is essentially equivalent to version 1.5.4. 28 00:02:27,290 --> 00:02:30,950 So that's all we have to do in our cargo file. 29 00:02:30,950 --> 00:02:33,590 So now we can start looking inside of Main. 30 00:02:34,410 --> 00:02:37,350 And we can start thinking about our program structure. 31 00:02:38,010 --> 00:02:45,150 So our program is going to take four arguments a string to search for, a string to replace it with 32 00:02:45,150 --> 00:02:52,770 an input file and an output file so we can keep all of these arguments together by grouping them into 33 00:02:52,770 --> 00:02:55,350 and arguments struck. 34 00:02:56,920 --> 00:03:00,580 And all of our fields are going to be stringed. 35 00:03:00,590 --> 00:03:03,490 So we're going to have a pattern, which is what we're looking for. 36 00:03:03,790 --> 00:03:11,200 We're going to have replace, which is what we're going to substitute in for our pattern. 37 00:03:11,320 --> 00:03:18,220 And then we're going to have our input file and our output file. 38 00:03:20,060 --> 00:03:25,520 So to help us debug, we're actually going to add in an attribute called debug. 39 00:03:26,030 --> 00:03:32,660 So we're going to derive debug and this is just going to allow us to use. 40 00:03:34,280 --> 00:03:39,980 Standard output, a little better to kind of format how we want to be able to print out our argument 41 00:03:39,990 --> 00:03:43,700 struck to be able to see the values associated with our field. 42 00:03:43,700 --> 00:03:47,990 So it's just going to help us kind of debug our struc. 43 00:03:48,910 --> 00:03:55,780 And now we see that we have a bunch of warnings already for having dead code. 44 00:03:55,780 --> 00:04:01,690 So I actually want to suppress these that way when we output to the terminal, we don't get a ton of 45 00:04:01,810 --> 00:04:03,520 warnings outputted with it. 46 00:04:03,520 --> 00:04:06,670 I'd like to keep our output to the terminal pretty clean. 47 00:04:07,360 --> 00:04:09,760 And that's what allow dead code does. 48 00:04:09,970 --> 00:04:15,670 So now that we have our struct set up, let's just go ahead and bring in our text polarizer and we'll 49 00:04:15,670 --> 00:04:20,410 just bring in everything that's within the crate, which is what that wild card is going to do for us. 50 00:04:20,410 --> 00:04:25,270 It tells it to just make everything inside that crate available to us. 51 00:04:27,100 --> 00:04:34,270 So now we can create a new function and we'll call it print help. 52 00:04:35,680 --> 00:04:39,040 And in here, we're going to use a different form of print line. 53 00:04:39,580 --> 00:04:43,540 And it's going to be the print line macro. 54 00:04:43,540 --> 00:04:49,240 And the E print line is going to print to the standard air with the new line. 55 00:04:49,240 --> 00:04:56,320 So it is equivalent to print line, except the output goes to standard air instead of standard out. 56 00:04:57,450 --> 00:05:01,480 We should only use e print line for air and progress messages. 57 00:05:01,500 --> 00:05:05,220 Otherwise you should continue to use print line instead. 58 00:05:05,340 --> 00:05:14,820 So in here, let's just start adding in some messages to help the user understand how to use our program. 59 00:05:15,090 --> 00:05:17,430 So what is our program going to do? 60 00:05:17,460 --> 00:05:24,240 It's going to replace a string with a new string. 61 00:05:26,310 --> 00:05:33,960 And then our program, because we put in our curly brackets, it's going to be called find and replace. 62 00:05:33,960 --> 00:05:38,310 And then we want it to be green. 63 00:05:38,310 --> 00:05:41,370 So that's how we change the color in this case. 64 00:05:42,210 --> 00:05:46,860 And then we also want to print out how to use our program. 65 00:05:49,420 --> 00:05:52,180 So we'll say usage 66 00:05:54,550 --> 00:05:56,530 target string. 67 00:05:58,510 --> 00:06:07,420 Replacement string input file and then we'll have out put. 68 00:06:09,750 --> 00:06:10,260 File. 69 00:06:12,010 --> 00:06:13,660 And that's all we need to do. 70 00:06:13,690 --> 00:06:16,000 So let's come up here. 71 00:06:16,540 --> 00:06:18,190 Let's call our function. 72 00:06:18,460 --> 00:06:21,040 And now let's come down here and run it. 73 00:06:21,040 --> 00:06:23,830 And it's going to build our dependencies for us. 74 00:06:25,410 --> 00:06:31,490 Now we see that we have find and replace and our green text that we specified replace a string with 75 00:06:31,490 --> 00:06:32,240 the new string. 76 00:06:32,240 --> 00:06:38,150 And then we see that it has usage target string replacement string input file and output file. 77 00:06:38,300 --> 00:06:44,080 So everything looks good and everything was printed out the way it should be. 78 00:06:44,090 --> 00:06:49,730 So in the next lecture, let's look at how we can take in user arguments from the command line.