1 00:00:00,000 --> 00:00:08,470 [No audio] 2 00:00:08,471 --> 00:00:11,502 This section is about basic programming with Rust. 3 00:00:11,503 --> 00:00:13,672 If you are already familiar with some other 4 00:00:13,673 --> 00:00:16,152 programming language, then this section will give you 5 00:00:16,153 --> 00:00:18,814 a quick start for writing codes in Rust. 6 00:00:18,815 --> 00:00:21,048 You don't need to worry if you are new 7 00:00:21,049 --> 00:00:23,832 as we will explain everything in detail and will 8 00:00:23,833 --> 00:00:26,536 learn everything in a step by step fashion from 9 00:00:26,537 --> 00:00:30,086 complete scratch. In this very first tutorial, 10 00:00:30,087 --> 00:00:33,136 in this section, we will learn two things. 11 00:00:33,137 --> 00:00:36,134 That is comments and basic print commands. 12 00:00:36,135 --> 00:00:38,620 So without any delay, let's get started. 13 00:00:39,470 --> 00:00:41,776 Almost every programming language support 14 00:00:41,777 --> 00:00:43,270 the use of comments. 15 00:00:43,271 --> 00:00:47,146 A comment is a programmer readable, explanation or annotation 16 00:00:47,147 --> 00:00:49,876 in the source code file of a computer program 17 00:00:49,877 --> 00:00:52,132 and they are written with the intention of making 18 00:00:52,133 --> 00:00:54,160 the source code easier to understand. 19 00:00:55,170 --> 00:00:58,590 They are ignored by compilers and are not executed. 20 00:00:59,410 --> 00:01:02,088 Commented code lines in Rust files are 21 00:01:02,089 --> 00:01:04,590 lines that starts with two backslashes. 22 00:01:04,591 --> 00:01:06,328 For instance, this line is going 23 00:01:06,329 --> 00:01:07,910 to be treated as comments. 24 00:01:07,911 --> 00:01:11,390 [No audio] 25 00:01:11,391 --> 00:01:13,878 We can have more comments in the code by adding 26 00:01:13,879 --> 00:01:17,050 more lines that starts with a couple of backslashes. 27 00:01:17,051 --> 00:01:22,880 [No audio] 28 00:01:22,881 --> 00:01:25,170 There are situations in which we may 29 00:01:25,171 --> 00:01:27,804 want to add multiple lines of comments. 30 00:01:27,805 --> 00:01:30,396 In that situation, it is not very convenient 31 00:01:30,397 --> 00:01:32,854 and effective to keep on adding backslashes at 32 00:01:32,855 --> 00:01:34,690 the start of each and every line. 33 00:01:35,300 --> 00:01:38,352 An efficient way of writing multiline comments in Rust 34 00:01:38,353 --> 00:01:40,710 is with the help of backslash and star. 35 00:01:41,480 --> 00:01:43,178 The comment is ended with the 36 00:01:43,179 --> 00:01:45,584 help of star followed by backslash. 37 00:01:45,585 --> 00:01:48,500 Let us add a single multiline comment. 38 00:01:48,501 --> 00:01:53,430 [No audio] 39 00:01:53,431 --> 00:01:55,768 These two types of comments are enough for now 40 00:01:55,769 --> 00:01:59,692 to add necessary documentation and explanation to our code. 41 00:01:59,693 --> 00:02:01,708 We will now cover the second topic of 42 00:02:01,709 --> 00:02:03,522 the lecture which is to learn the basic 43 00:02:03,523 --> 00:02:06,170 print command and some of its variants. 44 00:02:06,171 --> 00:02:08,482 We have already seen the basic print command 45 00:02:08,483 --> 00:02:11,100 in our first Rust program of Hello World. 46 00:02:11,870 --> 00:02:14,518 It is used to print something through the terminal. 47 00:02:14,519 --> 00:02:16,512 Let me add a simple print statement for 48 00:02:16,513 --> 00:02:18,750 printing the text of Hello from Rust. 49 00:02:18,751 --> 00:02:23,690 [No audio] 50 00:02:23,691 --> 00:02:27,250 Please note that ideally you should put semicolons 51 00:02:27,251 --> 00:02:29,850 after the end of each line of code. 52 00:02:29,851 --> 00:02:32,236 Of course, there are some exceptions which will become 53 00:02:32,237 --> 00:02:34,700 evident to us as we progress along the course. 54 00:02:35,390 --> 00:02:38,496 The ln after the print means that after 55 00:02:38,497 --> 00:02:41,142 this command, any subsequent printing to the terminal 56 00:02:41,143 --> 00:02:42,620 will be on the next line. 57 00:02:43,310 --> 00:02:45,094 The exclamation mark is a symbol 58 00:02:45,095 --> 00:02:47,178 to denote a Rust macro. 59 00:02:47,179 --> 00:02:49,268 In simple words, a macro are a 60 00:02:49,269 --> 00:02:51,556 way of writing code that writes other 61 00:02:51,557 --> 00:02:55,330 code which is also known as metaprogramming. 62 00:02:55,331 --> 00:02:57,892 The Rust macros are substituted by other 63 00:02:57,893 --> 00:03:01,010 code before the program is being executed. 64 00:03:01,011 --> 00:03:03,432 Don't worry about them for now as they will make 65 00:03:03,433 --> 00:03:06,168 sense as we progress along the course and cover the 66 00:03:06,169 --> 00:03:09,110 topic of macros in the later on sections. 67 00:03:09,111 --> 00:03:12,446 For now, it is sufficient to know that it is a macro. 68 00:03:12,447 --> 00:03:14,994 To execute the code, we will typically use the cargo 69 00:03:14,995 --> 00:03:17,990 run command by typing cargo run in the terminal. 70 00:03:18,650 --> 00:03:21,538 This command will execute the program and any output 71 00:03:21,539 --> 00:03:24,410 from the program will be displayed on the terminal. 72 00:03:24,411 --> 00:03:26,430 [No audio] 73 00:03:26,431 --> 00:03:28,256 Comments may be inserted within 74 00:03:28,257 --> 00:03:30,758 a Rust statement or command. 75 00:03:30,759 --> 00:03:32,806 It is done with the help of syntax 76 00:03:32,807 --> 00:03:35,494 that we use to write multiline comments. 77 00:03:35,495 --> 00:03:38,522 For instance, consider the Hello World print statement 78 00:03:38,523 --> 00:03:42,084 again. We can command a piece of this statement to 79 00:03:42,085 --> 00:03:44,778 be treated as comment and not part of the source 80 00:03:44,779 --> 00:03:48,370 code by enclosing it inside the multiline comment. 81 00:03:48,371 --> 00:03:51,490 The ln in this case will be treated as comment. 82 00:03:52,470 --> 00:03:55,816 Unlike println command, which moves to the next line 83 00:03:55,817 --> 00:03:58,920 after printing to the terminal, the print does not move 84 00:03:58,921 --> 00:04:01,602 to the next line after printing to the terminal. 85 00:04:01,603 --> 00:04:04,600 Let us now learn how to print strings and numbers. 86 00:04:04,601 --> 00:04:08,945 [No audio] 87 00:04:08,946 --> 00:04:12,092 We cannot print strings or numbers by 88 00:04:12,093 --> 00:04:14,642 just enclosing it inside the print statement. 89 00:04:14,643 --> 00:04:16,970 It will generate an error message. 90 00:04:16,971 --> 00:04:18,822 This is a nice feature of Rust 91 00:04:18,823 --> 00:04:21,390 which is known as compile time errors. 92 00:04:21,391 --> 00:04:23,408 The compile time errors refers to the 93 00:04:23,409 --> 00:04:25,488 error messages that are indicated by the 94 00:04:25,489 --> 00:04:28,830 compiler before we even run the program. 95 00:04:28,831 --> 00:04:31,172 If I move my mouse cursor, it will 96 00:04:31,173 --> 00:04:34,210 give me more details about the error message. 97 00:04:34,211 --> 00:04:36,052 In this case, it says you might 98 00:04:36,053 --> 00:04:38,770 be missing a string literal to format. 99 00:04:38,771 --> 00:04:42,282 If for some reason I do not understand the details 100 00:04:42,283 --> 00:04:44,632 that are being provided by the compiler, so I may 101 00:04:44,633 --> 00:04:47,938 execute this program and it will give me more information. 102 00:04:47,939 --> 00:04:49,830 [No audio] 103 00:04:49,831 --> 00:04:51,512 In this case, it is giving me the 104 00:04:51,513 --> 00:04:54,444 correct syntax indicated by the green text. 105 00:04:54,445 --> 00:04:55,836 Let us explain the problem 106 00:04:55,837 --> 00:04:59,356 in this case. The print function needs 107 00:04:59,357 --> 00:05:01,442 some sort of placeholder inside the double 108 00:05:01,443 --> 00:05:03,388 quotes which will be replaced using the 109 00:05:03,389 --> 00:05:07,150 constants, strings, variables or whatever we mention. 110 00:05:07,151 --> 00:05:09,856 So the correct syntax for printing out the value 111 00:05:09,857 --> 00:05:13,078 of ten will be placeholder inside the double quotes. 112 00:05:13,079 --> 00:05:14,576 Let me add some suitable text 113 00:05:14,577 --> 00:05:18,288 also. During the execution, the placeholder will be 114 00:05:18,289 --> 00:05:20,560 replaced with the value that we have mentioned. 115 00:05:21,490 --> 00:05:23,572 The output in this case will be the 116 00:05:23,573 --> 00:05:27,018 value is ten. If there are multiple placeholders 117 00:05:27,019 --> 00:05:29,546 and multiple values after the double quotes, 118 00:05:29,547 --> 00:05:31,684 so the placeholder will be replaced in the 119 00:05:31,685 --> 00:05:34,510 order in which the parameters are being provided. 120 00:05:34,511 --> 00:05:37,940 The order is always going to be from left to right. 121 00:05:38,550 --> 00:05:40,980 Let us look at printing the strings now. 122 00:05:41,670 --> 00:05:44,642 Strings are indicated with the help of double quotes. 123 00:05:44,643 --> 00:05:46,252 Let us add a print statement for 124 00:05:46,253 --> 00:05:48,268 printing my first name and last name 125 00:05:48,269 --> 00:05:51,190 which are being provided as string arguments. 126 00:05:51,191 --> 00:05:57,990 [No audio] 127 00:05:57,991 --> 00:06:00,584 This statement will print, My first name is 128 00:06:00,585 --> 00:06:03,750 Nouman and my last name is Azam. 129 00:06:04,490 --> 00:06:07,260 The first placeholder was being replaced with the first 130 00:06:07,261 --> 00:06:10,338 parameter after the double quotes and the second placeholder 131 00:06:10,339 --> 00:06:12,550 has been replaced with the second parameter. 132 00:06:13,470 --> 00:06:16,688 To print text on the same line and not on a 133 00:06:16,689 --> 00:06:20,390 new line, we will use the print statement without the ln. 134 00:06:20,391 --> 00:06:22,170 Let me add a print statement. 135 00:06:22,171 --> 00:06:27,170 [No audio] 136 00:06:27,171 --> 00:06:30,164 This text will now be printed on the same line. 137 00:06:30,165 --> 00:06:32,458 Please note that the print will only occur 138 00:06:32,459 --> 00:06:34,088 on the next line when there is no 139 00:06:34,089 --> 00:06:36,312 more spaces to print on the same line. 140 00:06:36,313 --> 00:06:38,980 Let me add some printing statements to see this. 141 00:06:38,981 --> 00:06:45,040 [No audio] 142 00:06:45,041 --> 00:06:46,402 Let us now look at another 143 00:06:46,403 --> 00:06:47,942 print statement. In this case 144 00:06:47,943 --> 00:06:50,268 now we have inserted text inside the print 145 00:06:50,269 --> 00:06:52,998 statement which is spanning more than one line. 146 00:06:52,999 --> 00:06:56,022 We also have a print statement with no 147 00:06:56,023 --> 00:06:59,418 text which will ensure that subsequent printing will 148 00:06:59,419 --> 00:07:01,306 be performed on the next line. 149 00:07:01,307 --> 00:07:03,270 Let us see the output in this case. 150 00:07:03,271 --> 00:07:05,640 [No audio] 151 00:07:05,641 --> 00:07:07,168 You may note that the output 152 00:07:07,169 --> 00:07:09,136 is also spanning multiple lines, 153 00:07:09,137 --> 00:07:12,528 although we have used this simple print statement. 154 00:07:12,529 --> 00:07:15,265 The key point here is that the print or println will 155 00:07:15,266 --> 00:07:18,318 print what it is instructed to print, and 156 00:07:18,319 --> 00:07:20,862 when the printing is complete, the println command will 157 00:07:20,863 --> 00:07:24,136 print subsequent text on next line, while the simple print 158 00:07:24,137 --> 00:07:27,280 will print subsequent printing on the same line. 159 00:07:27,281 --> 00:07:30,423 We will now look at the use of escape sequences. 160 00:07:30,424 --> 00:07:32,480 [No audio] 161 00:07:32,481 --> 00:07:36,012 An escape sequence contains a backslash symbol followed 162 00:07:36,013 --> 00:07:39,020 by one of the escape sequence characters. 163 00:07:39,021 --> 00:07:41,638 It is a combination of characters that has a 164 00:07:41,639 --> 00:07:45,700 meaning other than the literal character contained therein. 165 00:07:45,701 --> 00:07:48,742 We will cover a few of the most important ones. 166 00:07:48,743 --> 00:07:51,632 The first one in this regards is the backslash 167 00:07:51,633 --> 00:07:54,432 n, which is used to ensure that subsequent printing 168 00:07:54,433 --> 00:07:56,520 will be done on the next line. 169 00:07:56,521 --> 00:07:59,342 Adding it two times will mean that one line 170 00:07:59,343 --> 00:08:02,010 will be empty and will contain no text. 171 00:08:02,620 --> 00:08:04,570 Let me add some text also. 172 00:08:04,571 --> 00:08:08,030 [No audio] 173 00:08:08,031 --> 00:08:10,220 Let us confirm the output now. 174 00:08:10,221 --> 00:08:13,390 [No audio] 175 00:08:13,391 --> 00:08:16,048 There are other escape sequences also such as 176 00:08:16,049 --> 00:08:18,938 slash t, which is for inserting a tab. 177 00:08:18,939 --> 00:08:22,618 A tab is essentially spaces of some fixed length. 178 00:08:22,619 --> 00:08:25,680 Let us use it inside a print statement with some text. 179 00:08:25,681 --> 00:08:31,520 [No audio] 180 00:08:31,521 --> 00:08:33,308 Let us see the output now. 181 00:08:33,309 --> 00:08:35,679 [No audio] 182 00:08:35,680 --> 00:08:37,778 You may note some empty spaces at the 183 00:08:37,779 --> 00:08:40,558 beginning of the output on the terminal. 184 00:08:40,559 --> 00:08:43,416 Let us see one more frequently used escape sequence. 185 00:08:43,417 --> 00:08:46,502 The slash r is used for carriage return, which 186 00:08:46,503 --> 00:08:49,446 has its history related to the typewriter which people 187 00:08:49,447 --> 00:08:52,204 used in the old days for typing out documents. 188 00:08:52,205 --> 00:08:55,222 A carriage return moves your carriage all the way 189 00:08:55,223 --> 00:08:57,142 to the right at the start of the line. 190 00:08:57,143 --> 00:09:00,122 So anything that you print afterwards will delete any 191 00:09:00,123 --> 00:09:03,160 previous text that is present on the same line. 192 00:09:03,161 --> 00:09:05,162 Let me add a print statement with some 193 00:09:05,163 --> 00:09:07,860 text and the carriage return escape sequence. 194 00:09:07,861 --> 00:09:13,110 [No audio] 195 00:09:13,111 --> 00:09:15,356 The Rust will first print the text 196 00:09:15,357 --> 00:09:17,610 before the slash r on the screen. 197 00:09:17,611 --> 00:09:20,898 When it encounters slash r, it will move to the beginning 198 00:09:20,899 --> 00:09:23,936 of the same line and will write the remaining text after 199 00:09:23,937 --> 00:09:26,540 the slash r in the print command on the screen. 200 00:09:27,150 --> 00:09:28,838 This will erase the previously 201 00:09:28,839 --> 00:09:30,592 written text on the screen. 202 00:09:30,593 --> 00:09:32,620 Let us see the output of this. 203 00:09:32,621 --> 00:09:35,870 [No audio] 204 00:09:35,871 --> 00:09:39,316 We may note that we are only able to see the text 205 00:09:39,317 --> 00:09:43,268 of, this text will only appear on the screen, and we are 206 00:09:43,269 --> 00:09:46,850 unable to see the first part of the text on the terminal. 207 00:09:46,851 --> 00:09:48,772 A backslash is very useful when 208 00:09:48,773 --> 00:09:50,730 used inside the print statement. 209 00:09:50,731 --> 00:09:52,376 It is used to ignore the effect 210 00:09:52,377 --> 00:09:55,070 of one escape sequence which comes afterwards 211 00:09:55,071 --> 00:09:57,304 and will print it as ordinary text. 212 00:09:57,305 --> 00:09:59,598 For instance, let us add a couple of slash 213 00:09:59,599 --> 00:10:03,000 n inside a print statement with some text. 214 00:10:03,001 --> 00:10:07,670 [No audio] 215 00:10:07,671 --> 00:10:09,832 The first slash n has 216 00:10:09,833 --> 00:10:12,072 an additional backslash before it. 217 00:10:12,073 --> 00:10:15,016 This will ignore its special meaning and therefore the first 218 00:10:15,017 --> 00:10:17,128 slash n will be part of the text, while the 219 00:10:17,129 --> 00:10:19,762 second slash n will have its special meaning. 220 00:10:19,763 --> 00:10:21,350 Let us see the output. 221 00:10:21,351 --> 00:10:23,411 [No audio] 222 00:10:23,450 --> 00:10:26,242 The next use of the backslash is to display 223 00:10:26,243 --> 00:10:29,302 single quotes and double quotes inside the print statement. 224 00:10:29,303 --> 00:10:31,900 Let me add a print statement to explain this. 225 00:10:31,901 --> 00:10:35,230 [No audio] 226 00:10:35,231 --> 00:10:37,488 In this print statement, a single quote will 227 00:10:37,489 --> 00:10:39,974 be displayed in place of the backslash quote, 228 00:10:39,975 --> 00:10:41,908 while a double quote will be displayed in 229 00:10:41,909 --> 00:10:44,770 place of the backslash double quote. 230 00:10:44,771 --> 00:10:48,308 The double quotes in this case will not be 231 00:10:48,309 --> 00:10:51,812 treated as the ending double quotes corresponding to the 232 00:10:51,813 --> 00:10:54,574 starting double quotes of the print statement. 233 00:10:54,575 --> 00:10:56,026 Let us see its output. 234 00:10:57,830 --> 00:11:00,408 The backslash special meaning can be used to 235 00:11:00,409 --> 00:11:03,406 print the backslash itself inside the print statement. 236 00:11:03,407 --> 00:11:05,784 We will use two backslashes in this case. 237 00:11:05,785 --> 00:11:08,191 Let me add a print statement to illustrate this. 238 00:11:08,192 --> 00:11:12,837 [No audio] 239 00:11:12,838 --> 00:11:16,378 In this example now, the first backslash will ignore the 240 00:11:16,379 --> 00:11:18,682 special meaning of the second backslash and 241 00:11:18,683 --> 00:11:21,216 therefore the second backslash will be printed. 242 00:11:21,217 --> 00:11:23,806 One of the main uses of the backslash where 243 00:11:23,807 --> 00:11:25,732 it will be used as part of the printing 244 00:11:25,733 --> 00:11:28,680 text is to display the directory or folder locations. 245 00:11:29,660 --> 00:11:32,700 Next we will look at the positional arguments. 246 00:11:32,701 --> 00:11:35,272 The positional arguments are a list of parameters 247 00:11:35,273 --> 00:11:37,128 or arguments that we pass to the print 248 00:11:37,129 --> 00:11:40,140 command after the double quote separated by commas. 249 00:11:40,720 --> 00:11:44,540 Let me add a print text with some positional arguments. 250 00:11:44,541 --> 00:11:50,840 [No audio] 251 00:11:50,841 --> 00:11:53,732 In this statement, the numbers inside the placeholder 252 00:11:53,733 --> 00:11:56,388 will be replaced by the respective parameters. 253 00:11:56,389 --> 00:11:59,182 The first parameter, which is the string of like, 254 00:11:59,183 --> 00:12:02,206 will be replaced by the placeholder containing one. 255 00:12:02,207 --> 00:12:04,494 The second parameter, which has the value of 256 00:12:04,495 --> 00:12:07,288 20, will be replaced by the placeholder containing 257 00:12:07,289 --> 00:12:08,962 the value of two, and in the same 258 00:12:08,963 --> 00:12:11,496 way the third parameter will also be replaced. 259 00:12:11,497 --> 00:12:13,281 Let us see the respective outputs. 260 00:12:13,282 --> 00:12:16,281 [No audio] 261 00:12:16,320 --> 00:12:18,424 Sometimes it may be more meaningful 262 00:12:18,425 --> 00:12:20,246 to use names instead of numbers. 263 00:12:20,247 --> 00:12:22,582 This means that we will use the names of the 264 00:12:22,583 --> 00:12:26,660 arguments instead of using their numbers inside the placeholders. 265 00:12:26,661 --> 00:12:28,838 Let us look at an example by including a 266 00:12:28,839 --> 00:12:32,020 suitable print statement containing a couple of named arguments. 267 00:12:32,021 --> 00:12:38,530 [No audio] 268 00:12:38,531 --> 00:12:40,804 The first placeholder will contain the value of 269 00:12:40,805 --> 00:12:42,788 the parameter with the name of language and 270 00:12:42,789 --> 00:12:44,884 the second parameter will contain the value of 271 00:12:44,885 --> 00:12:47,410 the parameter with the name of activity. 272 00:12:47,411 --> 00:12:49,760 The order does not matter in this case. 273 00:12:50,930 --> 00:12:52,948 Let us look at one last topic and that 274 00:12:52,949 --> 00:12:55,514 is printing basic Math inside a print statement. 275 00:12:55,515 --> 00:12:57,532 I will add a print statement in this case 276 00:12:57,533 --> 00:12:59,590 which will print the value of some addition. 277 00:12:59,591 --> 00:13:03,900 [No audio] 278 00:13:03,901 --> 00:13:06,318 The placeholder in this example will be 279 00:13:06,319 --> 00:13:08,760 replaced with the value of addition operation. 280 00:13:09,340 --> 00:13:12,302 There are a whole bunch of other details which we 281 00:13:12,303 --> 00:13:14,462 will only cover when they become relevant to us 282 00:13:14,463 --> 00:13:16,852 in the course. We will be covering 283 00:13:16,853 --> 00:13:18,868 basic data types in the next lecture, 284 00:13:18,869 --> 00:13:20,302 so do come back for learning that, 285 00:13:20,303 --> 00:13:23,000 and until then, happy Rust programming. 286 00:13:23,001 --> 00:13:28,768 [No audio]