1 00:00:00,180 --> 00:00:04,800 In this lecture, we are going to start our journey by learning about variables. 2 00:00:05,100 --> 00:00:08,730 This course assumes you have some experience with programming. 3 00:00:09,150 --> 00:00:14,850 Learning a new language is much easier the second time around, while the syntax may be different. 4 00:00:15,030 --> 00:00:18,390 The concepts are very similar from language to language. 5 00:00:18,690 --> 00:00:20,700 Variables are still variables. 6 00:00:20,940 --> 00:00:22,710 Functions are still functions. 7 00:00:22,950 --> 00:00:27,300 Conditional statements are still conditional statements, so on and so forth. 8 00:00:27,540 --> 00:00:34,360 You get the picture variables in rust serve the same purpose as variables in other languages. 9 00:00:34,680 --> 00:00:41,430 Their containers for data throughout this course, we will be working inside these source slash main 10 00:00:41,730 --> 00:00:42,630 RC file. 11 00:00:42,900 --> 00:00:46,290 I recommend opening this file before starting a lecture. 12 00:00:46,830 --> 00:00:50,820 Inside this file cargo has defined a function called Main. 13 00:00:51,300 --> 00:00:55,950 I know we said we would start with variables, but let me quickly introduce functions. 14 00:00:56,250 --> 00:01:00,270 Functions are blocks of code for performing a particular task. 15 00:01:00,540 --> 00:01:06,930 If you have a background in JavaScript, you would use the function key word to define a function for 16 00:01:06,930 --> 00:01:08,220 Python developers. 17 00:01:08,340 --> 00:01:12,420 You would use the defined keyword for US developers. 18 00:01:12,540 --> 00:01:15,630 Function definitions start with the FN keyword. 19 00:01:15,840 --> 00:01:16,800 It's all the same. 20 00:01:17,370 --> 00:01:20,640 The FN keyword is followed by the name of the function. 21 00:01:20,670 --> 00:01:23,550 A list of arguments and then the lock of code. 22 00:01:23,970 --> 00:01:26,730 Our apps must define a function called mean. 23 00:01:27,180 --> 00:01:32,250 By default, Cargoe will search for a function called mean during compilation. 24 00:01:32,640 --> 00:01:36,690 If we were to rename this function, the compiler would complain. 25 00:01:36,960 --> 00:01:39,540 It's always guaranteed this function will run. 26 00:01:39,540 --> 00:01:44,790 Once we're going to empty, the contents of the function will be starting fresh. 27 00:01:45,150 --> 00:01:47,940 The main focus of this lecture is variables. 28 00:01:48,180 --> 00:01:51,780 We can declare a variable by typing the let keyword. 29 00:01:54,330 --> 00:01:58,590 Next, we can give our variable a name for this example. 30 00:01:58,620 --> 00:02:02,370 Let's set the name to my name for the value. 31 00:02:02,400 --> 00:02:06,360 We're going to store our real name or whatever name you prefer. 32 00:02:09,169 --> 00:02:13,340 After declaring this variable rust is going to throw a warning. 33 00:02:13,670 --> 00:02:20,060 Assuming you have the rust extension enabled, there should be a yellow squiggly line below the variable 34 00:02:20,060 --> 00:02:20,480 name. 35 00:02:20,810 --> 00:02:25,910 We can hover our mouse over the variable to learn about the error just in case. 36 00:02:25,910 --> 00:02:32,000 If you don't see an error, you can produce the error by running the cargo run command in the terminal. 37 00:02:34,520 --> 00:02:39,060 The compiler won't hesitate to output the same error regardless. 38 00:02:39,140 --> 00:02:43,100 The error message should be the name, according to the compiler. 39 00:02:43,190 --> 00:02:45,320 There's a problem with the variable name. 40 00:02:45,650 --> 00:02:49,100 At times, rust can be an opinionated language. 41 00:02:49,370 --> 00:02:51,900 Variables must be snake case. 42 00:02:52,130 --> 00:02:53,810 They can't be camel cased. 43 00:02:54,110 --> 00:02:56,690 It's a standard practice in rust projects. 44 00:02:56,960 --> 00:02:59,990 Let's convert the variable name to snake casing. 45 00:03:02,620 --> 00:03:08,620 Learning to adopt a specific formatting practice can be tedious, especially if you're used to writing 46 00:03:08,620 --> 00:03:09,580 code differently. 47 00:03:09,880 --> 00:03:13,330 However, it's worth it to be consistent with the community. 48 00:03:13,690 --> 00:03:16,480 There are a couple more features I want to point out. 49 00:03:17,230 --> 00:03:20,500 Firstly, statements must end with a semicolon. 50 00:03:20,770 --> 00:03:25,660 If you have a background in JavaScript or Python, semicolons are optional. 51 00:03:25,960 --> 00:03:31,090 Statements in rust must end with a semicolon to indicate the end of the line. 52 00:03:31,480 --> 00:03:35,980 If we were to remove this character, the compiler would throw an error. 53 00:03:36,400 --> 00:03:42,430 Errors are indicated in the editor with a red squiggly line, according to the compiler. 54 00:03:42,550 --> 00:03:44,170 It expected a semicolon. 55 00:03:44,440 --> 00:03:45,820 Let's add it back in. 56 00:03:46,270 --> 00:03:51,790 Moving along, another warning gets thrown by the compiler surrounding the variable. 57 00:03:52,150 --> 00:03:55,150 The error will tell us the variable is unused. 58 00:03:55,450 --> 00:03:58,420 For this example, we are going to ignore this warning. 59 00:03:58,660 --> 00:04:00,160 It's not the end of the world. 60 00:04:00,760 --> 00:04:01,270 All right. 61 00:04:01,390 --> 00:04:06,280 One more thing before we move on to the next topic variables are immutable. 62 00:04:08,860 --> 00:04:15,350 Mutability is another way of saying a variable can't be changed once a variable is initialized. 63 00:04:15,370 --> 00:04:20,019 We can't change it to another value, therefore they're immutable. 64 00:04:20,470 --> 00:04:25,390 This design forces developers to be aware of how their variables will be used. 65 00:04:25,720 --> 00:04:28,000 That doesn't mean mutability is bad. 66 00:04:28,270 --> 00:04:31,180 It is possible to make a variable mutable. 67 00:04:33,720 --> 00:04:38,000 Let's try mutating the my name variable to a different value. 68 00:04:40,510 --> 00:04:42,820 An error gets thrown by the compiler. 69 00:04:43,030 --> 00:04:46,390 It's telling us we can't modify an immutable variable. 70 00:04:46,720 --> 00:04:52,420 Variables can become mutable by adding the multi keyword before the variable name. 71 00:04:52,720 --> 00:04:55,720 Let's add this keyword to the my name variable. 72 00:04:58,240 --> 00:05:01,180 With a single keyword, the air has gone away. 73 00:05:01,690 --> 00:05:06,220 Mutable variables must be explicitly declared with the mute keyword. 74 00:05:06,520 --> 00:05:09,430 You should keep this in mind when working with variables. 75 00:05:09,700 --> 00:05:13,120 Let's continue talking about rust in the next lecture.