1 00:00:06,600 --> 00:00:08,880 - Okay, so in this section we're going to see how to build 2 00:00:08,880 --> 00:00:11,250 and run our first Rust application. 3 00:00:11,250 --> 00:00:13,020 Here's the code we're going to compile. 4 00:00:13,020 --> 00:00:16,560 It's hello world.rs simple main function. 5 00:00:16,560 --> 00:00:18,300 It calls the print ln macro, 6 00:00:18,300 --> 00:00:20,610 prints hello world on the console. 7 00:00:20,610 --> 00:00:22,280 What could be easier than that? 8 00:00:22,280 --> 00:00:26,280 Just a reminder, if you want to do these demos for yourself 9 00:00:26,280 --> 00:00:28,680 you can go to the Rust dev demo folder. 10 00:00:28,680 --> 00:00:30,690 Lesson 01 getting started. 11 00:00:30,690 --> 00:00:34,110 That's the folder for the demos for this first lesson. 12 00:00:34,110 --> 00:00:35,913 So let's just confirm that. 13 00:00:36,900 --> 00:00:37,890 So here we are. 14 00:00:37,890 --> 00:00:42,120 I'm in the Rust dev lesson 01 getting started folder. 15 00:00:42,120 --> 00:00:45,450 Here's the simple application, hello world.rs. 16 00:00:45,450 --> 00:00:47,640 Later on in this lesson we're also going to see 17 00:00:47,640 --> 00:00:51,480 how to create, build and run an application using Cargo. 18 00:00:51,480 --> 00:00:52,350 We'll see that later 19 00:00:52,350 --> 00:00:55,290 but we'll just focus on the simple example for now. 20 00:00:55,290 --> 00:00:57,930 Let me just open that up in the code editor. 21 00:00:57,930 --> 00:00:59,160 So here we are. 22 00:00:59,160 --> 00:01:00,570 I'm using Visual Studio Code. 23 00:01:00,570 --> 00:01:04,650 As I was saying previously, other editors are available. 24 00:01:04,650 --> 00:01:08,880 Sublime, Eclipse, VIM, anything you fancy really. 25 00:01:08,880 --> 00:01:11,160 If you are going to use Visual Studio Code 26 00:01:11,160 --> 00:01:13,170 there are extensions available too 27 00:01:13,170 --> 00:01:15,690 so that you can actually run the application directly 28 00:01:15,690 --> 00:01:17,970 within the Visual Studio Code environment. 29 00:01:17,970 --> 00:01:22,020 And I was tempted to illustrate that in the video series 30 00:01:22,020 --> 00:01:23,850 but I decided not to 31 00:01:23,850 --> 00:01:25,860 because you might be using a different tool 32 00:01:25,860 --> 00:01:27,540 and I didn't want to kind of mandate 33 00:01:27,540 --> 00:01:30,690 or make you feel like you had to use Visual Studio Code. 34 00:01:30,690 --> 00:01:32,760 But if you are interested, have a look 35 00:01:32,760 --> 00:01:36,300 at the extensions and download the extensions 36 00:01:36,300 --> 00:01:40,290 for Rust and it'll definitely help your experience. 37 00:01:40,290 --> 00:01:43,020 What I'm going to do is I'm just gonna compile 38 00:01:43,020 --> 00:01:46,023 the application from a normal command window. 39 00:01:46,890 --> 00:01:48,630 So let's see how to do this. 40 00:01:48,630 --> 00:01:50,043 Just open a command window, 41 00:01:51,140 --> 00:01:52,590 go to the folder where your code is 42 00:01:52,590 --> 00:01:55,080 and then run the Rust compiler rustc. 43 00:01:55,080 --> 00:01:57,150 Give it the name of your application. 44 00:01:57,150 --> 00:01:58,692 Quite straightforward, really. 45 00:01:58,692 --> 00:01:59,970 What it'll generate 46 00:01:59,970 --> 00:02:03,780 obviously the executable, your application that you can run. 47 00:02:03,780 --> 00:02:08,220 If you're on Windows, it also generates a PDB file. 48 00:02:08,220 --> 00:02:11,460 This is a like a programming debug information 49 00:02:11,460 --> 00:02:14,310 so that it helps you single step through the application. 50 00:02:14,310 --> 00:02:19,310 It contains symbolic debug symbols to help you 51 00:02:19,380 --> 00:02:23,070 with your kind of diagnostic debug sessions. 52 00:02:23,070 --> 00:02:24,630 Okay, well let me do that. 53 00:02:24,630 --> 00:02:25,890 I'm running Windows 54 00:02:25,890 --> 00:02:28,743 so let's run the Rust compiler on my machine. 55 00:02:29,700 --> 00:02:30,660 Okay, so here we are. 56 00:02:30,660 --> 00:02:31,800 I've opened the folder. 57 00:02:31,800 --> 00:02:34,350 I've opened the command window in the right folder. 58 00:02:34,350 --> 00:02:38,663 So let me just run the Rust compiler, hello_world.rs 59 00:02:42,000 --> 00:02:46,050 and it will generate, let's see what it's generated. 60 00:02:46,050 --> 00:02:48,600 The executable application, which is great 61 00:02:48,600 --> 00:02:51,750 and the the PDB file as well. 62 00:02:51,750 --> 00:02:52,583 So that's great. 63 00:02:52,583 --> 00:02:54,000 So I could run the application. 64 00:02:54,000 --> 00:02:57,930 Now I'll just show you how, look, if I had an error message. 65 00:02:57,930 --> 00:02:59,430 So if we go back in the code 66 00:02:59,430 --> 00:03:03,210 let's say I was a Python developer and in Python 67 00:03:03,210 --> 00:03:05,850 instead of saying fn, you say def. 68 00:03:05,850 --> 00:03:10,320 So maybe I was thinking about Python when I wrote my code. 69 00:03:10,320 --> 00:03:12,780 Okay and maybe I really was thinking about Python 70 00:03:12,780 --> 00:03:14,490 and I had a colon instead 71 00:03:14,490 --> 00:03:17,430 of curly brackets because that's how it would be in Python. 72 00:03:17,430 --> 00:03:19,380 So lots of errors here. 73 00:03:19,380 --> 00:03:22,230 I wonder what the compiler would make of that. 74 00:03:22,230 --> 00:03:23,733 Let's try it again. 75 00:03:26,790 --> 00:03:30,480 Okay, so there's an error online one. 76 00:03:30,480 --> 00:03:32,370 Oh, that was a bad start. 77 00:03:32,370 --> 00:03:35,340 It's expecting some kind of simple 78 00:03:35,340 --> 00:03:39,000 the main function or this is the scope operator. 79 00:03:39,000 --> 00:03:40,600 We'll have a look at that later. 80 00:03:41,700 --> 00:03:45,870 So it's clearly got confused, but then look at this 81 00:03:45,870 --> 00:03:49,530 it gives me helpful error messages, shock, horror. 82 00:03:49,530 --> 00:03:51,330 Maybe you should write fn instead 83 00:03:51,330 --> 00:03:53,640 of def to declare a function. 84 00:03:53,640 --> 00:03:56,370 Well, yes, maybe I should. 85 00:03:56,370 --> 00:03:59,520 So let's fix that, save it. 86 00:03:59,520 --> 00:04:01,857 Go back into command window and recompile. 87 00:04:03,690 --> 00:04:05,543 I wonder what we're gonna get this time. 88 00:04:06,900 --> 00:04:07,830 Oh dear, oh dear. 89 00:04:07,830 --> 00:04:10,020 It's confused about the the colon. 90 00:04:10,020 --> 00:04:13,710 It was expecting this colon to be a curly bracket perhaps 91 00:04:13,710 --> 00:04:16,530 or maybe we'll have a look at the syntax later on. 92 00:04:16,530 --> 00:04:20,190 The arrow syntax here is how you can specify the return type 93 00:04:20,190 --> 00:04:23,460 from a function you could select arrow int, for example 94 00:04:23,460 --> 00:04:25,350 if it was meant to return an integer. 95 00:04:25,350 --> 00:04:29,100 So it's got horribly confused at this point. 96 00:04:29,100 --> 00:04:32,940 Usually when you get multiple errors, you ignore everything 97 00:04:32,940 --> 00:04:34,260 apart from the first one. 98 00:04:34,260 --> 00:04:37,440 Fix that and then hope that it fixes the others. 99 00:04:37,440 --> 00:04:42,150 So I realize that this is a Rust program 100 00:04:42,150 --> 00:04:43,920 not a Python program. 101 00:04:43,920 --> 00:04:46,077 I save the file and I recompile. 102 00:04:48,980 --> 00:04:51,780 And we're back in the room. 103 00:04:51,780 --> 00:04:53,880 So the application is ready to run. 104 00:04:53,880 --> 00:04:55,773 To run it, let's see how to run it. 105 00:04:56,670 --> 00:04:57,780 So to run the application 106 00:04:57,780 --> 00:05:02,010 if you are on Linux or Mac, just run it like so. 107 00:05:02,010 --> 00:05:05,010 And if you run it on Windows like I am, 108 00:05:05,010 --> 00:05:06,930 then you just run it like that 109 00:05:06,930 --> 00:05:10,320 with a back slash instead of a forward slash. 110 00:05:10,320 --> 00:05:14,820 So .\hello world. 111 00:05:14,820 --> 00:05:17,010 Of course I could say hello world.xe on Windows 112 00:05:17,010 --> 00:05:18,010 but I don't need to. 113 00:05:18,990 --> 00:05:20,280 And it was well worth the wait. 114 00:05:20,280 --> 00:05:21,660 So hello world. 115 00:05:21,660 --> 00:05:23,310 Marvelous. 116 00:05:23,310 --> 00:05:25,607 Just a quick reminder, I talked about this in the 117 00:05:25,607 --> 00:05:29,760 one of the earlier sessions, the Rust Playground, 118 00:05:29,760 --> 00:05:34,440 an online editor where you can play with Rust syntax 119 00:05:34,440 --> 00:05:37,140 without having to actually install it on your machine. 120 00:05:37,140 --> 00:05:39,990 Most languages these days have some kind of playground 121 00:05:39,990 --> 00:05:43,050 an online editor for you to have a play. 122 00:05:43,050 --> 00:05:47,070 So for Rust, this is the Rust playground window. 123 00:05:47,070 --> 00:05:49,320 Let's have a quick look at that. 124 00:05:49,320 --> 00:05:50,989 So here it is. 125 00:05:50,989 --> 00:05:54,120 This is the code that was actually in the Rust playground 126 00:05:54,120 --> 00:05:55,560 when I opened it. 127 00:05:55,560 --> 00:05:59,130 And it looks fine to me, so I can run it. 128 00:05:59,130 --> 00:06:03,600 In fact, I'll miss off a curly bracket. 129 00:06:03,600 --> 00:06:08,600 I'll have a deliberate error here and I'll try to run it. 130 00:06:09,180 --> 00:06:12,120 So the compiler runs online 131 00:06:12,120 --> 00:06:13,560 and the error message gets report here. 132 00:06:13,560 --> 00:06:17,190 It's the same compiler, but in an online kind of experience 133 00:06:17,190 --> 00:06:22,110 and it's complaining about an unclosed delimiter. 134 00:06:22,110 --> 00:06:27,110 The curly bracket that started here is not closed here. 135 00:06:27,219 --> 00:06:29,340 Oh, I see what I've done wrong. 136 00:06:29,340 --> 00:06:30,390 I forgot my curly bracket. 137 00:06:30,390 --> 00:06:31,440 Let's try that again. 138 00:06:32,370 --> 00:06:35,170 The compiler when you do online can be a little bit slow 139 00:06:36,180 --> 00:06:38,460 because it's kind of like a remote experience. 140 00:06:38,460 --> 00:06:41,400 So you have to be a little bit patient 141 00:06:41,400 --> 00:06:42,480 when you're running it. 142 00:06:42,480 --> 00:06:44,850 So I'm just gonna wait for it to be compiled 143 00:06:44,850 --> 00:06:46,770 and it has compiled successfully. 144 00:06:46,770 --> 00:06:49,170 And here's the output from the application. 145 00:06:49,170 --> 00:06:52,320 Okay, so I'm not going to be using the playground a lot 146 00:06:52,320 --> 00:06:53,220 during the demos 147 00:06:53,220 --> 00:06:56,190 because the code we've got is gonna be more significant 148 00:06:56,190 --> 00:06:58,590 and we'll kind of do it properly. 149 00:06:58,590 --> 00:07:00,870 But if you haven't yet got the compiler installed, 150 00:07:00,870 --> 00:07:03,770 you can always try examples here just as a starting point.