1 00:00:00,320 --> 00:00:02,780 In this video, we'll discuss working 2 00:00:02,780 --> 00:00:05,060 effectively with Cargo fmt and Cargo 3 00:00:05,060 --> 00:00:07,790 clippy. Cargo fmt is a really useful 4 00:00:07,790 --> 00:00:10,070 tool for Auto formatting your Rust code 5 00:00:10,070 --> 00:00:12,790 according to Rust style guidelines. If 6 00:00:12,790 --> 00:00:15,679 you get an error when you run Cargo 7 00:00:15,679 --> 00:00:17,990 fmt, then you need to install the Rust 8 00:00:17,990 --> 00:00:21,350 format component using Rust up, it looks 9 00:00:21,350 --> 00:00:24,940 like this. 10 00:00:24,940 --> 00:00:27,190 Rust fmt is most likely already 11 00:00:27,190 --> 00:00:29,020 installed so you probably won't have to 12 00:00:29,020 --> 00:00:31,840 run that step. Once its installed 13 00:00:31,840 --> 00:00:35,350 Cargo fmt will do its job, Cargo 14 00:00:35,350 --> 00:00:38,260 fmt is usually silent what it does is 15 00:00:38,260 --> 00:00:40,480 auto format your code. Let's go take a 16 00:00:40,480 --> 00:00:44,050 look here's our code, and it's already 17 00:00:44,050 --> 00:00:46,540 properly formatted, so let's mess it up a 18 00:00:46,540 --> 00:00:50,829 bit. 19 00:00:50,829 --> 00:00:52,840 Okay that's a bit of an odd way to 20 00:00:52,840 --> 00:00:55,899 format things, let's go and run Cargo 21 00:00:55,899 --> 00:00:58,440 fmt and see what happens. 22 00:00:58,440 --> 00:01:00,000 Now I'll go back to our code 23 00:01:00,000 --> 00:01:03,300 again and we'll see that Cargo fmt 24 00:01:03,300 --> 00:01:06,440 moved it back to the correct format. This is 25 00:01:06,440 --> 00:01:09,530 super useful to avoid arguments about 26 00:01:09,530 --> 00:01:12,650 where parens go, wear braces go, how you 27 00:01:12,650 --> 00:01:15,140 should format things. There's one 28 00:01:15,140 --> 00:01:17,090 suggested way to do it, there's one 29 00:01:17,090 --> 00:01:19,400 built-in tool to do it, if you just use 30 00:01:19,400 --> 00:01:21,530 it no one has to have any arguments, 31 00:01:21,530 --> 00:01:23,480 it's just however the tool does it, and 32 00:01:23,480 --> 00:01:25,280 it looks pretty good. 33 00:01:25,280 --> 00:01:28,940 Okay what about Cargo clippy? This might 34 00:01:28,940 --> 00:01:31,400 also already be installed on your system 35 00:01:31,400 --> 00:01:33,710 but if it isn't just run Rust up 36 00:01:33,710 --> 00:01:36,200 component add clippy, and Rust up will 37 00:01:36,200 --> 00:01:39,380 install it for you. While Cargo fmt 38 00:01:39,380 --> 00:01:40,610 deals with style, 39 00:01:40,610 --> 00:01:43,160 clippy is a collection of lints to catch 40 00:01:43,160 --> 00:01:44,870 common mistakes and help you improve 41 00:01:44,870 --> 00:01:48,150 your Rust code. Let's try it out 42 00:01:48,150 --> 00:01:50,640 let's start by looking at the second 43 00:01:50,640 --> 00:01:53,760 warning use of unwrapped or followed by 44 00:01:53,760 --> 00:01:56,370 a call to new. So the problem here is 45 00:01:56,370 --> 00:01:59,550 that string new will happen, whether or 46 00:01:59,550 --> 00:02:02,340 not the value is needed. So we're going 47 00:02:02,340 --> 00:02:04,410 to do an allocation and create a new 48 00:02:04,410 --> 00:02:06,630 string, whether or not we actually need 49 00:02:06,630 --> 00:02:09,030 the new string. So this is suggesting 50 00:02:09,030 --> 00:02:12,930 instead call unwrap or default, and then 51 00:02:12,930 --> 00:02:14,880 inside the method it will create a 52 00:02:14,880 --> 00:02:16,950 default for a string, which is a blank 53 00:02:16,950 --> 00:02:18,840 string only if it's needed. 54 00:02:18,840 --> 00:02:21,570 So that's an optimization, so let's go 55 00:02:21,570 --> 00:02:25,209 fix that. 56 00:02:25,209 --> 00:02:27,819 OK we've made the fix, let's go run 57 00:02:27,819 --> 00:02:30,620 Cargill clippy again. 58 00:02:30,620 --> 00:02:32,989 All right we fixed that item as we can 59 00:02:32,989 --> 00:02:34,489 see it doesn't show up anymore, let's 60 00:02:34,489 --> 00:02:37,549 look at the next one. Length comparison 61 00:02:37,549 --> 00:02:40,610 to zero so we're using dot Len to get a 62 00:02:40,610 --> 00:02:42,409 length, and we're comparing it to zero 63 00:02:42,409 --> 00:02:45,290 and we're told using naught is empty is 64 00:02:45,290 --> 00:02:47,989 clearer and more explicit. Okay 65 00:02:47,989 --> 00:02:50,840 so they want us to put a not in front, 66 00:02:50,840 --> 00:02:53,959 and use is empty instead of Len, so let's 67 00:02:53,959 --> 00:02:58,200 go make that change. 68 00:02:58,200 --> 00:03:00,090 Alright let's go around Cargo clippy 69 00:03:00,090 --> 00:03:02,050 again, 70 00:03:02,050 --> 00:03:04,600 great clippy couldn't find anything to 71 00:03:04,600 --> 00:03:07,210 complain about. I suggest using Cargo 72 00:03:07,210 --> 00:03:09,610 fmt and Cargo clippy, either in a 73 00:03:09,610 --> 00:03:12,070 continuous integration check, or put it 74 00:03:12,070 --> 00:03:14,160 in something like a Git pre-commit hook. 75 00:03:14,160 --> 00:03:16,990 I'll give an example of using this in a 76 00:03:16,990 --> 00:03:20,020 Git pre-commit hook. The file to create 77 00:03:20,020 --> 00:03:25,620 is dot Git hooks pre-commit. 78 00:03:25,620 --> 00:03:28,430 I'm going to paste in my preferred setup. 79 00:03:28,430 --> 00:03:30,870 This file will be interpreted as 80 00:03:30,870 --> 00:03:33,540 Shell code. This will make it so that 81 00:03:33,540 --> 00:03:35,489 before get makes a commit it will run 82 00:03:35,489 --> 00:03:37,440 Cargo fmt, and then let it do its 83 00:03:37,440 --> 00:03:40,410 thing and then it will run Cargo clippy 84 00:03:40,410 --> 00:03:42,690 and abort the commit if any warnings are 85 00:03:42,690 --> 00:03:46,379 present. The - D warnings turns the 86 00:03:46,379 --> 00:03:48,569 warnings into errors, which will cause 87 00:03:48,569 --> 00:03:51,000 the hook to fail. We can go see it in 88 00:03:51,000 --> 00:03:53,610 action if I go add a formatting problem 89 00:03:53,610 --> 00:03:55,379 and do something that will make clippy 90 00:03:55,379 --> 00:03:58,770 complain, but first let's save this, 91 00:03:58,770 --> 00:04:02,890 and let's make it executable. 92 00:04:02,890 --> 00:04:06,680 And now let's go at our changes. 93 00:04:06,680 --> 00:04:08,900 Here is a change that Cargo fmt will 94 00:04:08,900 --> 00:04:11,590 revert, 95 00:04:11,590 --> 00:04:13,629 and here's some code that clippy will 96 00:04:13,629 --> 00:04:16,480 complain about. Let's try to commit our 97 00:04:16,480 --> 00:04:21,430 code and see what happens. 98 00:04:21,430 --> 00:04:24,040 Okay so clippy complained about the 99 00:04:24,040 --> 00:04:27,399 linting error, what about Cargo fmt? If 100 00:04:27,399 --> 00:04:29,500 we go back we see Cargo fmt did its 101 00:04:29,500 --> 00:04:33,849 thing, so then we fix the linting error, 102 00:04:33,849 --> 00:04:36,129 and now when we try to commit it should 103 00:04:36,129 --> 00:04:39,980 work. 104 00:04:39,980 --> 00:04:42,470 In the next video, we'll talk about how 105 00:04:42,470 --> 00:04:47,470 to handle errors gracefully.