1 00:00:07,000 --> 00:00:07,833 - In the previous section, 2 00:00:07,833 --> 00:00:11,580 we saw how to create a sample application by using cargo. 3 00:00:11,580 --> 00:00:15,210 It's located here in the rustdev demo folder, 4 00:00:15,210 --> 00:00:19,083 lesson_01_getting_started/hello_world_via_cargo. 5 00:00:20,010 --> 00:00:22,563 So here it is in my file system, rustdev, 6 00:00:23,480 --> 00:00:27,120 lesson_01_getting_started/hello_world_via_cargo. 7 00:00:27,120 --> 00:00:28,320 If I open that file, 8 00:00:28,320 --> 00:00:32,940 if I open that directory in Visual Studio Code, here it is. 9 00:00:32,940 --> 00:00:35,820 Quick reminder, we had the TOML file, 10 00:00:35,820 --> 00:00:37,920 which contains information about our package 11 00:00:37,920 --> 00:00:40,701 or our application plus any dependencies 12 00:00:40,701 --> 00:00:42,150 that our application might have, 13 00:00:42,150 --> 00:00:43,830 we don't have any at the moment. 14 00:00:43,830 --> 00:00:48,830 By the way, TOML stands for Tom's Obvious Minimal Language. 15 00:00:49,779 --> 00:00:51,570 There we go, fun fact. 16 00:00:51,570 --> 00:00:53,250 And in the source folder, of course, 17 00:00:53,250 --> 00:00:56,190 we just had our simple main application like so. 18 00:00:56,190 --> 00:00:57,960 So let's see how to build the application 19 00:00:57,960 --> 00:01:00,420 and how to run it using cargo. 20 00:01:00,420 --> 00:01:01,803 Let's see how to do that. 21 00:01:03,060 --> 00:01:07,560 Okay, so you can either build the application in debug mode 22 00:01:07,560 --> 00:01:09,810 or you can build it in release mode. 23 00:01:09,810 --> 00:01:14,010 If you build it in debug mode, then the compile is fast, 24 00:01:14,010 --> 00:01:17,550 it isn't optimized, so it's not very fast at runtime, 25 00:01:17,550 --> 00:01:19,560 but it's fast at compile time 26 00:01:19,560 --> 00:01:21,630 and it contains debug information. 27 00:01:21,630 --> 00:01:23,550 And so, you know, 9 times out of 10, 28 00:01:23,550 --> 00:01:26,010 you'll be running the application in debug mode 29 00:01:26,010 --> 00:01:27,750 until you've completely debugged it 30 00:01:27,750 --> 00:01:30,420 and then you can build it in release mode later 31 00:01:30,420 --> 00:01:33,480 when it's guaranteed to be working. 32 00:01:33,480 --> 00:01:36,240 So to build the application in debug mode, 33 00:01:36,240 --> 00:01:38,100 open up a command window, 34 00:01:38,100 --> 00:01:42,150 it'll generate in the ./target/debug directory, 35 00:01:42,150 --> 00:01:45,660 that's where your application will end up being placed. 36 00:01:45,660 --> 00:01:46,950 So this is how you do it, 37 00:01:46,950 --> 00:01:49,290 from the command window cargo build. 38 00:01:49,290 --> 00:01:50,973 So I'm going to do that now. 39 00:01:51,900 --> 00:01:54,930 Here's a command window opened in the correct directory 40 00:01:54,930 --> 00:01:58,773 from my application, cargo build. 41 00:02:01,830 --> 00:02:06,830 Okay, and it has generated the application in debug mode. 42 00:02:07,440 --> 00:02:11,763 So if I do a direct research on the target subdirectory, 43 00:02:13,290 --> 00:02:16,773 you'll notice that there's a debug subdirectory, 44 00:02:18,175 --> 00:02:22,200 and in there, in target\debug, 45 00:02:22,200 --> 00:02:26,070 there's the application hello_world_via_cargo plus lots 46 00:02:26,070 --> 00:02:29,220 of other files that are useful for debugging purposes. 47 00:02:29,220 --> 00:02:30,340 So basically all you have to do 48 00:02:30,340 --> 00:02:35,340 right now is to run the hello_world_via_cargo executable 49 00:02:35,430 --> 00:02:37,353 from the target debug folder. 50 00:02:39,150 --> 00:02:40,860 So to do that, it obviously depends 51 00:02:40,860 --> 00:02:43,320 whether you're using Linux, Mac, or Windows 52 00:02:43,320 --> 00:02:46,470 because the slashes go one way or the other way. 53 00:02:46,470 --> 00:02:50,430 I'm running Windows so target, backslash, debug, 54 00:02:50,430 --> 00:02:52,623 backslash, hello_world_via_cargo, 55 00:03:00,870 --> 00:03:05,163 there we go, .\target\debug\hello_world_via_cargo.exe. 56 00:03:06,810 --> 00:03:08,130 Hello, world! 57 00:03:08,130 --> 00:03:11,070 Okay, so that's the application built in debug mode. 58 00:03:11,070 --> 00:03:12,570 If you want to do the same in release mode, 59 00:03:12,570 --> 00:03:15,060 when you're confident that the application is, you know, 60 00:03:15,060 --> 00:03:18,300 debugged and correct and ready to run in production, 61 00:03:18,300 --> 00:03:20,880 then you can build it in release mode. 62 00:03:20,880 --> 00:03:24,510 The build is slower because the compiler's working harder 63 00:03:24,510 --> 00:03:26,460 to optimize your code. 64 00:03:26,460 --> 00:03:28,380 It doesn't contain any debug information 65 00:03:28,380 --> 00:03:30,960 but the application will be faster at runtime, 66 00:03:30,960 --> 00:03:33,270 which is obviously what you want in production. 67 00:03:33,270 --> 00:03:34,650 So it generates the application 68 00:03:34,650 --> 00:03:37,350 in the target release folder, you might have guessed. 69 00:03:37,350 --> 00:03:39,330 This is how you build in release mode, 70 00:03:39,330 --> 00:03:41,700 cargo build --release. 71 00:03:41,700 --> 00:03:42,800 So let me do that now. 72 00:03:47,340 --> 00:03:51,840 Okay, so it has built the release version of the application 73 00:03:51,840 --> 00:03:54,300 optimized for fast execution at runtime 74 00:03:54,300 --> 00:03:57,603 and it is in the target release folder. 75 00:04:00,810 --> 00:04:02,970 Okay, so in the target release folder, 76 00:04:02,970 --> 00:04:06,423 I have my hello_world_via_cargo executable. 77 00:04:08,610 --> 00:04:11,850 So to run it again, depending whether you run in Linux, 78 00:04:11,850 --> 00:04:15,540 or Mac or Windows, go to the target release folder, 79 00:04:15,540 --> 00:04:17,243 run the application. 80 00:04:17,243 --> 00:04:22,243 So here I am, .\target\release slash 81 00:04:24,630 --> 00:04:25,463 hello_world_via_cargo.exe. 82 00:04:28,763 --> 00:04:29,700 Of course it still prints the same application, 83 00:04:29,700 --> 00:04:31,650 but this application is running faster. 84 00:04:32,520 --> 00:04:34,320 Okay, so you can build the application 85 00:04:34,320 --> 00:04:36,483 and then you can run it as separate steps. 86 00:04:37,410 --> 00:04:38,610 Alternatively, you can build 87 00:04:38,610 --> 00:04:41,100 and run the application in one go. 88 00:04:41,100 --> 00:04:43,890 Okay, so you can, 89 00:04:43,890 --> 00:04:45,720 this is probably what you'd wanna do in practice. 90 00:04:45,720 --> 00:04:48,930 In reality, if the application has already been compiled, 91 00:04:48,930 --> 00:04:51,540 then it won't recompile it, it'll just run it. 92 00:04:51,540 --> 00:04:54,120 This is what you do, you say cargo run. 93 00:04:54,120 --> 00:04:58,350 So that will build the application in debug mode by default 94 00:04:58,350 --> 00:04:59,183 if it needs to, 95 00:04:59,183 --> 00:05:01,770 if the application hasn't already been built in debug mode, 96 00:05:01,770 --> 00:05:05,520 if we've changed it, it'll rebuild it and then run it. 97 00:05:05,520 --> 00:05:07,283 Okay, in debug mode 98 00:05:07,283 --> 00:05:11,640 or in release mode, you can say cargo run --release. 99 00:05:11,640 --> 00:05:14,580 Again, it'll build the application in release mode 100 00:05:14,580 --> 00:05:15,540 if it needs to, 101 00:05:15,540 --> 00:05:17,940 and then it run the application in release mode. 102 00:05:19,290 --> 00:05:23,130 So cargo run would build if it needs to in debug mode 103 00:05:23,130 --> 00:05:25,050 and then run it in debug mode. 104 00:05:25,050 --> 00:05:26,970 You can see it's run the debug version 105 00:05:26,970 --> 00:05:28,220 of the application there. 106 00:05:29,070 --> 00:05:32,880 Alternatively, I can say run the application, 107 00:05:32,880 --> 00:05:36,030 build and run, really in release mode, 108 00:05:36,030 --> 00:05:37,260 so now it's gonna build 109 00:05:37,260 --> 00:05:39,300 and run the application in release mode. 110 00:05:39,300 --> 00:05:43,080 So again, in practice, rather than building the application, 111 00:05:43,080 --> 00:05:45,300 and then running it as two separate steps, 112 00:05:45,300 --> 00:05:47,580 in reality you just say cargo run, 113 00:05:47,580 --> 00:05:50,190 it'll build the application if it needs to and then run it. 114 00:05:50,190 --> 00:05:51,540 So that's what we'll be doing from now on. 115 00:05:51,540 --> 00:05:52,950 We'll just say cargo run 116 00:05:52,950 --> 00:05:56,419 for every application for the rest of the video series. 117 00:05:56,419 --> 00:05:58,620 One other thing that's worth mentioning, 118 00:05:58,620 --> 00:06:01,680 there's also a command called cargo check. 119 00:06:01,680 --> 00:06:05,080 So this just does a sanity check on your syntax. 120 00:06:05,080 --> 00:06:08,220 It doesn't attempt to generate an executable, 121 00:06:08,220 --> 00:06:10,050 it's just checking your syntax to make sure 122 00:06:10,050 --> 00:06:11,880 that you haven't missed any semicolons 123 00:06:11,880 --> 00:06:14,940 or curly brackets or you know, any typo like this. 124 00:06:14,940 --> 00:06:17,730 So if you just want check the sanity of your code, 125 00:06:17,730 --> 00:06:20,130 this is faster than building it, 126 00:06:20,130 --> 00:06:23,400 because it doesn't have to generate any executable. 127 00:06:23,400 --> 00:06:25,050 So for example, in here, 128 00:06:25,050 --> 00:06:29,100 if I accidentally on purpose forgot a curly bracket, 129 00:06:29,100 --> 00:06:31,230 and I just wanted to check the syntax, 130 00:06:31,230 --> 00:06:32,973 I can say cargo check. 131 00:06:35,880 --> 00:06:37,620 So let's do that now, 132 00:06:37,620 --> 00:06:40,380 I wonder if my code is correct. 133 00:06:40,380 --> 00:06:43,740 Oh, it isn't, unclosed delimiter, 134 00:06:43,740 --> 00:06:48,461 that delimiter there was not closed here. 135 00:06:48,461 --> 00:06:52,323 Okay, so therefore let me go and fix the error. 136 00:06:53,250 --> 00:06:55,743 I see my error, curly bracket added, 137 00:06:56,670 --> 00:06:58,353 let's do a cargo check again. 138 00:07:00,840 --> 00:07:03,600 And now it has compiled successfully. 139 00:07:03,600 --> 00:07:06,240 It hasn't generated a runnable application, 140 00:07:06,240 --> 00:07:08,160 but at least I know my application is fine. 141 00:07:08,160 --> 00:07:10,350 So that concludes this lesson. 142 00:07:10,350 --> 00:07:12,720 We've seen basic syntax for Rust. 143 00:07:12,720 --> 00:07:14,730 We've also introduced the cargo tool 144 00:07:14,730 --> 00:07:16,770 as a way of generating the application 145 00:07:16,770 --> 00:07:18,600 and then building it and running it. 146 00:07:18,600 --> 00:07:20,430 So for the rest of the video series, 147 00:07:20,430 --> 00:07:21,600 all we need to do now is look 148 00:07:21,600 --> 00:07:23,793 at the syntax for the Rust language itself.