1 00:00:06,540 --> 00:00:07,560 - So in this lesson, 2 00:00:07,560 --> 00:00:10,650 we are going to dig deeper into Rust syntax. 3 00:00:10,650 --> 00:00:12,420 And the first thing we need to do really 4 00:00:12,420 --> 00:00:15,330 is to see how to declare variables. 5 00:00:15,330 --> 00:00:18,510 So it turns out that Rust has integers and floating points 6 00:00:18,510 --> 00:00:21,240 and strings and booleans and characters, 7 00:00:21,240 --> 00:00:23,910 all the things you'd expect in a language. 8 00:00:23,910 --> 00:00:25,710 The syntax for declaring variables 9 00:00:25,710 --> 00:00:27,900 is a little bit different from what you might be used to 10 00:00:27,900 --> 00:00:31,620 if you come from a background in C or C++ or Java. 11 00:00:31,620 --> 00:00:33,600 So let's see what's available. 12 00:00:33,600 --> 00:00:34,710 So first of all, 13 00:00:34,710 --> 00:00:38,745 these are the signed into the types available in Rust. 14 00:00:38,745 --> 00:00:43,230 So we have an i8, that's a signed eight bit integer. 15 00:00:43,230 --> 00:00:48,230 So that would give you a value from -128 up to +127. 16 00:00:48,510 --> 00:00:50,250 We have i16. 17 00:00:50,250 --> 00:00:55,250 That would give you a signed integer from -32K up to +32K. 18 00:00:56,580 --> 00:00:59,757 And then we have a 32 bit, 64 bit, 19 00:00:59,757 --> 00:01:03,750 and 128 bit signed integers, negative or positive. 20 00:01:03,750 --> 00:01:06,600 So obviously, the idea is that you'd use, 21 00:01:06,600 --> 00:01:08,730 depending on the value that you wanna store, 22 00:01:08,730 --> 00:01:11,910 you would choose a big enough integer to hold that value. 23 00:01:11,910 --> 00:01:14,280 So these are signed integers. 24 00:01:14,280 --> 00:01:18,780 Rust also has unsigned integers, unlike Java, really, 25 00:01:18,780 --> 00:01:20,460 but like C and C++, 26 00:01:20,460 --> 00:01:22,050 we have unsigned integers as well. 27 00:01:22,050 --> 00:01:23,099 Same idea. 28 00:01:23,099 --> 00:01:26,220 U8 is an eight bit unsigned integer. 29 00:01:26,220 --> 00:01:29,429 That would give you a number between zero and 255. 30 00:01:29,429 --> 00:01:32,490 U16, unsigned 16 bit. 31 00:01:32,490 --> 00:01:35,850 That would give you a number between zero and 64K, 32 00:01:35,850 --> 00:01:36,780 and so on. 33 00:01:36,780 --> 00:01:38,490 Okay, so you could choose these types 34 00:01:38,490 --> 00:01:40,961 for an unsigned quantity. 35 00:01:40,961 --> 00:01:43,440 As well as the types we've just seen, 36 00:01:43,440 --> 00:01:48,440 there are also two other types, isize and usize. 37 00:01:48,570 --> 00:01:52,350 A signed integer and an unsigned integer 38 00:01:52,350 --> 00:01:55,770 whose size is the same as the natural word 39 00:01:55,770 --> 00:01:57,600 on your architecture. 40 00:01:57,600 --> 00:02:02,580 So for example, my computer has an eight byte word, 41 00:02:02,580 --> 00:02:04,230 that's the chip size. 42 00:02:04,230 --> 00:02:08,700 So isize and usize on my machine would be eight bytes. 43 00:02:08,700 --> 00:02:11,160 It's optimized for performance. 44 00:02:11,160 --> 00:02:14,010 If you want to make an application as fast as possible, 45 00:02:14,010 --> 00:02:15,600 then you should use these types 46 00:02:15,600 --> 00:02:18,720 because those are the most efficient types 47 00:02:18,720 --> 00:02:21,123 that your computer can can process. 48 00:02:22,440 --> 00:02:25,200 Okay, so I feel like we should have an example 49 00:02:25,200 --> 00:02:26,550 of all of this. 50 00:02:26,550 --> 00:02:30,090 So for each section in each chapter, 51 00:02:30,090 --> 00:02:32,190 I've got a separate demo. 52 00:02:32,190 --> 00:02:35,850 Each demo is a separate Cargo project. 53 00:02:35,850 --> 00:02:39,063 So I've got a Cargo project called lesson02_variables_types. 54 00:02:41,310 --> 00:02:44,640 And in that Cargo project, 55 00:02:44,640 --> 00:02:49,640 I've got various functions for each section of the lesson. 56 00:02:50,100 --> 00:02:53,460 For this section, the function is called demo_integers. 57 00:02:53,460 --> 00:02:54,300 So we'll have a look. 58 00:02:54,300 --> 00:02:56,550 We'll open this project in a moment 59 00:02:56,550 --> 00:02:58,650 and we'll have a look at this function. 60 00:02:58,650 --> 00:03:00,660 And once we've had looked through the code, 61 00:03:00,660 --> 00:03:03,897 then we'd run it using cargo run, okay? 62 00:03:03,897 --> 00:03:07,650 And we'll do that for each section in each lesson 63 00:03:07,650 --> 00:03:08,483 in the video series. 64 00:03:08,483 --> 00:03:10,920 It'll be the same kind of pattern. 65 00:03:10,920 --> 00:03:12,930 Okay, let's take a look. 66 00:03:12,930 --> 00:03:17,843 So here is my rustdev lesson02_variables_types. 67 00:03:17,843 --> 00:03:21,540 So lesson02_variables_types is a Cargo project. 68 00:03:21,540 --> 00:03:24,693 I basically said cargo new lesson02_variables_types. 69 00:03:26,010 --> 00:03:28,566 And it created this Cargo project. 70 00:03:28,566 --> 00:03:31,350 If I open that project in Visual Studio Code 71 00:03:31,350 --> 00:03:33,600 or whatever your editor happens to be, 72 00:03:33,600 --> 00:03:34,710 this is what it looks like. 73 00:03:34,710 --> 00:03:37,830 So the TOML file is pretty vanilla. 74 00:03:37,830 --> 00:03:39,960 And then the source folder, 75 00:03:39,960 --> 00:03:41,460 I've got a single file at the moment. 76 00:03:41,460 --> 00:03:44,100 Later on, we'll see how you can write applications 77 00:03:44,100 --> 00:03:45,393 with multiple files. 78 00:03:46,680 --> 00:03:48,750 At the moment, it's just a single file. 79 00:03:48,750 --> 00:03:51,957 And in here, I've got separate functions. 80 00:03:51,957 --> 00:03:55,890 So each function starts with an fn keyword. 81 00:03:55,890 --> 00:03:57,870 The main function chronologically 82 00:03:57,870 --> 00:04:00,483 is the first function that will always be executed. 83 00:04:01,410 --> 00:04:02,670 It doesn't matter the order 84 00:04:02,670 --> 00:04:05,610 in which you write functions in Rust. 85 00:04:05,610 --> 00:04:09,660 So this might be a surprise for a C++ developer. 86 00:04:09,660 --> 00:04:13,170 In C++, you've gotta kind of declare a function 87 00:04:13,170 --> 00:04:16,170 before you call it in the sequencing of your code. 88 00:04:16,170 --> 00:04:19,170 So in C++, you might, at the top of your file, 89 00:04:19,170 --> 00:04:20,700 have a function prototype 90 00:04:20,700 --> 00:04:23,370 to declare the signature of the function 91 00:04:23,370 --> 00:04:24,780 before you actually call it. 92 00:04:24,780 --> 00:04:26,280 And Rust doesn't need that. 93 00:04:26,280 --> 00:04:27,390 Basically, all the functions 94 00:04:27,390 --> 00:04:29,160 within the same kind of namespace. 95 00:04:29,160 --> 00:04:31,290 So the order that you write the functions in the file 96 00:04:31,290 --> 00:04:32,820 completely irrelevant. 97 00:04:32,820 --> 00:04:33,960 So chronologically, 98 00:04:33,960 --> 00:04:36,600 I decided to put the main function first. 99 00:04:36,600 --> 00:04:40,740 And at the moment, it's got calls to the other functions, 100 00:04:40,740 --> 00:04:43,457 which I'll show you later on, but they're all commented out. 101 00:04:43,457 --> 00:04:47,340 So I need to uncomment this function call 102 00:04:47,340 --> 00:04:50,130 so that it'll actually invoke that function. 103 00:04:50,130 --> 00:04:53,760 Let's take a look at that function, demo_integers. 104 00:04:53,760 --> 00:04:54,593 Here we go. 105 00:04:54,593 --> 00:04:57,570 So, obviously, this is a one-line comment. 106 00:04:57,570 --> 00:05:00,980 Rust supports multi-line comments using a / and a *. 107 00:05:02,760 --> 00:05:04,620 Okay, so that's the same kind of thing. 108 00:05:04,620 --> 00:05:08,850 You could say, blah, blah, blah, end multi-line comment. 109 00:05:08,850 --> 00:05:10,830 That's the same as most other languages. 110 00:05:10,830 --> 00:05:12,093 So let me just revert. 111 00:05:13,110 --> 00:05:14,760 Okay then, so what have I done here? 112 00:05:14,760 --> 00:05:16,266 Then I've declared, 113 00:05:16,266 --> 00:05:17,250 this is how you declare a variable in Rusty, 114 00:05:17,250 --> 00:05:20,280 say let, followed by the name of the variable, 115 00:05:20,280 --> 00:05:22,740 and then a :, and it's type, okay? 116 00:05:22,740 --> 00:05:24,840 So that's different from what you might've seen 117 00:05:24,840 --> 00:05:25,800 in other languages. 118 00:05:25,800 --> 00:05:29,040 So for example, in Java or C++ or C#, 119 00:05:29,040 --> 00:05:33,450 you just said something like a int x = 42, 120 00:05:33,450 --> 00:05:36,150 you'd have the type of the variable, then it's name, 121 00:05:36,150 --> 00:05:37,650 then it's value. 122 00:05:37,650 --> 00:05:39,870 It's back to front in Rust. 123 00:05:39,870 --> 00:05:42,450 Let is the variable declaration, 124 00:05:42,450 --> 00:05:45,030 name of the variable, :, type. 125 00:05:45,030 --> 00:05:47,850 So these are all 32 bit integers. 126 00:05:47,850 --> 00:05:51,360 The first value is decimal and it's a signed quantity, 127 00:05:51,360 --> 00:05:53,250 so I give it negative value. 128 00:05:53,250 --> 00:05:55,170 The next value is hexadecimal. 129 00:05:55,170 --> 00:05:57,690 0x is hexadecimal. 130 00:05:57,690 --> 00:06:00,120 0o is octo. 131 00:06:00,120 --> 00:06:03,480 If you like octo, I don't actually, but you might. 132 00:06:03,480 --> 00:06:05,580 And then 0b is binary. 133 00:06:05,580 --> 00:06:06,753 So there we go. 134 00:06:07,650 --> 00:06:10,830 I've got four signed integers. 135 00:06:10,830 --> 00:06:13,950 I've got an unsigned integer for a little bit of balance 136 00:06:13,950 --> 00:06:15,900 and I've given it a positive value. 137 00:06:15,900 --> 00:06:19,680 Incidentally, if you try to assign a negative value 138 00:06:19,680 --> 00:06:22,620 to an unsigned integer, you're gonna get a compiler error, 139 00:06:22,620 --> 00:06:24,630 which is what you'd expect. 140 00:06:24,630 --> 00:06:27,540 And then I've got an isize variable. 141 00:06:27,540 --> 00:06:28,920 I'm kind of curious to find out 142 00:06:28,920 --> 00:06:31,893 how big is an isize on my machine. 143 00:06:33,390 --> 00:06:35,220 Well, there's an isize variable, 144 00:06:35,220 --> 00:06:37,320 and then I've printed various things out. 145 00:06:37,320 --> 00:06:40,230 The println function is quite intuitive. 146 00:06:40,230 --> 00:06:43,740 Really, curly brackets here is a placeholder. 147 00:06:43,740 --> 00:06:45,720 So when it says curly brackets, 148 00:06:45,720 --> 00:06:49,230 it'll grab the first parameter at the end, a1, 149 00:06:49,230 --> 00:06:52,050 and it'll pass that value into here, 150 00:06:52,050 --> 00:06:54,030 and then it'll grab the next parameter 151 00:06:54,030 --> 00:06:56,670 and pass it into the next placeholder here, 152 00:06:56,670 --> 00:06:58,620 and then it'll grab the next parameter 153 00:06:58,620 --> 00:07:00,480 and pass it into this placeholder here. 154 00:07:00,480 --> 00:07:05,310 So it basically fills up a1, a2, a3, a4, 155 00:07:05,310 --> 00:07:09,600 and then b and c in the order that they appear here. 156 00:07:09,600 --> 00:07:12,690 Or you can display them in a different order. 157 00:07:12,690 --> 00:07:16,560 So here, technically speaking, this is parameter 0, 158 00:07:16,560 --> 00:07:19,950 and that's parameter 1, parameter 2, parameter 3, 159 00:07:19,950 --> 00:07:22,110 parameter 4, parameter 5. 160 00:07:22,110 --> 00:07:24,750 But I've displayed them in a different order. 161 00:07:24,750 --> 00:07:29,250 I've displayed parameter 0, which is that one at the end, 162 00:07:29,250 --> 00:07:32,790 and then I've displayed parameter 1 before it, okay? 163 00:07:32,790 --> 00:07:35,310 So actually, this print statement here, 164 00:07:35,310 --> 00:07:37,320 when I call println this example, 165 00:07:37,320 --> 00:07:39,120 it'll print the numbers in reverse order. 166 00:07:39,120 --> 00:07:43,350 It'll print parameter 5 first, so that'll be c. 167 00:07:43,350 --> 00:07:47,730 Then it'll print parameter 4, which is b, and so on. 168 00:07:47,730 --> 00:07:49,800 And then finally, notice this syntax. 169 00:07:49,800 --> 00:07:51,030 This is interesting syntax, 170 00:07:51,030 --> 00:07:54,060 and I'm gonna explain it in more detail later on. 171 00:07:54,060 --> 00:07:58,803 Effectively, stud::mem::size_of is a generic function. 172 00:07:59,850 --> 00:08:01,080 You've gotta give it a type, 173 00:08:01,080 --> 00:08:03,750 and it'll tell you how big that type is. 174 00:08:03,750 --> 00:08:06,870 The way you call generic functions in Rust 175 00:08:06,870 --> 00:08:10,350 is you say ::, and then inside angle brackets, 176 00:08:10,350 --> 00:08:12,630 you give it the type that you are interested in. 177 00:08:12,630 --> 00:08:14,730 So this is kind of similar 178 00:08:14,730 --> 00:08:18,900 if you are coming from a C#, C++ background, I should say, 179 00:08:18,900 --> 00:08:22,950 it's similar to same sizeof, something like that. 180 00:08:22,950 --> 00:08:25,410 It'll give you the number of bytes of that quantity. 181 00:08:25,410 --> 00:08:26,910 So when I run the application, 182 00:08:26,910 --> 00:08:30,420 this will tell me how many bytes is an isize. 183 00:08:30,420 --> 00:08:32,940 It's the natural word size on my machine, 184 00:08:32,940 --> 00:08:34,443 which I think is eight bytes. 185 00:08:35,430 --> 00:08:38,460 Okay, so let me just collapse the function down again. 186 00:08:38,460 --> 00:08:41,160 When I run the application, it'll call main, 187 00:08:41,160 --> 00:08:43,080 which will call my demo_integers. 188 00:08:43,080 --> 00:08:46,080 So to run the application in Visual Studio Code, 189 00:08:46,080 --> 00:08:48,106 I'm gonna open a terminal window 190 00:08:48,106 --> 00:08:50,820 in the current directory, okay? 191 00:08:50,820 --> 00:08:54,420 So the current folder, it'll open up a command prompt. 192 00:08:54,420 --> 00:08:57,960 It's a PowerShell command prompt in Windows. 193 00:08:57,960 --> 00:09:00,933 And in here, you can just say cargo run. 194 00:09:02,220 --> 00:09:04,683 So it'll build the application and then run it. 195 00:09:07,050 --> 00:09:09,150 Now an interesting, so first of all, 196 00:09:09,150 --> 00:09:11,280 the output is as expected. 197 00:09:11,280 --> 00:09:15,120 It output the numbers in kind of forward order. 198 00:09:15,120 --> 00:09:18,420 Then it output the numbers in reverse order. 199 00:09:18,420 --> 00:09:21,780 And then it told me that an isize is eight bytes 200 00:09:21,780 --> 00:09:22,613 on my machine. 201 00:09:22,613 --> 00:09:24,510 Okay, that's what I expected. 202 00:09:24,510 --> 00:09:27,633 But also notice I've got some warnings here. 203 00:09:28,950 --> 00:09:31,710 The Rust compiler is quite rigorous. 204 00:09:31,710 --> 00:09:33,270 If you have functions that you've written 205 00:09:33,270 --> 00:09:34,500 but you haven't actually called, 206 00:09:34,500 --> 00:09:37,920 it'll give you a warning, and it has. 207 00:09:37,920 --> 00:09:41,850 So it says you have a function called demo_floats 208 00:09:41,850 --> 00:09:44,370 but you've never used it, okay? 209 00:09:44,370 --> 00:09:46,740 So why did you write this function 210 00:09:46,740 --> 00:09:47,790 if you're never calling it? 211 00:09:47,790 --> 00:09:49,920 Did you forget to call it somewhere? 212 00:09:49,920 --> 00:09:51,300 No, not yet. 213 00:09:51,300 --> 00:09:53,550 It's just that I haven't got round to it. 214 00:09:53,550 --> 00:09:57,090 This mechanism here, we'll talk about later on. 215 00:09:57,090 --> 00:09:58,740 There are various different flags, 216 00:09:58,740 --> 00:10:02,220 effectively a compiler flag that you can embed in your code 217 00:10:02,220 --> 00:10:04,410 to influence the compilation process. 218 00:10:04,410 --> 00:10:07,200 And what it's saying here is that by default, 219 00:10:07,200 --> 00:10:11,250 the compiler is kind of running with this compiler flag. 220 00:10:11,250 --> 00:10:13,710 It will gimme a warning on dead_code. 221 00:10:13,710 --> 00:10:17,340 Code that exists, but is never called or used, 222 00:10:17,340 --> 00:10:20,280 you'll get a warning for dead_code, okay? 223 00:10:20,280 --> 00:10:21,840 You could disable that if you wanted to. 224 00:10:21,840 --> 00:10:23,580 We'll talk more about that later on. 225 00:10:23,580 --> 00:10:26,580 It's complaining about the demo_floats function 226 00:10:26,580 --> 00:10:29,520 and the demo_other_simple_types function. 227 00:10:29,520 --> 00:10:30,720 This one here. 228 00:10:30,720 --> 00:10:34,920 And also the demo_additional_techniques function here, okay? 229 00:10:34,920 --> 00:10:38,010 So in reality, obviously, you would be using these functions 230 00:10:38,010 --> 00:10:40,290 and those warnings wouldn't exist. 231 00:10:40,290 --> 00:10:41,580 Even though they were warnings, 232 00:10:41,580 --> 00:10:43,380 the application still ran 233 00:10:43,380 --> 00:10:46,260 and it output the information as expected. 234 00:10:46,260 --> 00:10:49,170 Okay, so we've seen how to declare integers. 235 00:10:49,170 --> 00:10:50,760 What we'll do in the next section 236 00:10:50,760 --> 00:10:52,320 is see how to declare floats 237 00:10:52,320 --> 00:10:54,030 and how do you use floating point values 238 00:10:54,030 --> 00:10:55,053 in your application?