1 00:00:00,000 --> 00:00:07,620 [No audio] 2 00:00:07,621 --> 00:00:09,464 Hello and welcome back. 3 00:00:09,465 --> 00:00:11,592 In this tutorial we will be looking at some 4 00:00:11,593 --> 00:00:15,326 basic stuff related to compiling and executing programs. 5 00:00:15,327 --> 00:00:18,530 I will make a new folder for saving all of my files. 6 00:00:18,531 --> 00:00:23,000 [No audio] 7 00:00:23,001 --> 00:00:24,906 Let me add a new file of 8 00:00:24,907 --> 00:00:27,264 Example1.rs to this folder. 9 00:00:27,265 --> 00:00:29,002 All the Rust code files needs 10 00:00:29,003 --> 00:00:31,460 to have an extension of .rs. 11 00:00:32,320 --> 00:00:34,418 Let us add a program similar to the one 12 00:00:34,419 --> 00:00:36,856 that we looked at in the previous tutorial. 13 00:00:36,857 --> 00:00:39,192 We will start by adding the main function. 14 00:00:40,960 --> 00:00:43,682 You may notice that as I write the code, there 15 00:00:43,683 --> 00:00:46,578 are some suggestions that are coming along and that is 16 00:00:46,579 --> 00:00:50,500 because of the extensions for us that we have installed. 17 00:00:50,501 --> 00:00:53,830 The main function is like an entry point into the code. 18 00:00:53,831 --> 00:00:56,838 When the compiler compiles our program, it will 19 00:00:56,839 --> 00:00:59,226 look for the main function and will start 20 00:00:59,227 --> 00:01:02,230 executing the code that is contained inside it. 21 00:01:02,920 --> 00:01:05,440 Next, when I put the starting parentheses, 22 00:01:05,441 --> 00:01:09,136 the ending parentheses are automatically being generated. 23 00:01:09,137 --> 00:01:11,066 This is quite handy and cool because 24 00:01:11,067 --> 00:01:14,380 it eliminates the chances of forgetting it. 25 00:01:14,381 --> 00:01:17,166 Next, I will add a print statement and we'll add the text 26 00:01:17,167 --> 00:01:20,360 of Hello World to it to be printed on the terminal. 27 00:01:21,740 --> 00:01:24,242 Again, you may have noticed that as I write 28 00:01:24,243 --> 00:01:26,674 there are some suggestions and the closing brackets and 29 00:01:26,675 --> 00:01:29,150 the quotes are being auto generated for us. 30 00:01:29,680 --> 00:01:32,434 Okay, this program is now complete. 31 00:01:32,435 --> 00:01:35,218 This small bubble is an indication that 32 00:01:35,219 --> 00:01:37,346 the program has not been saved yet. 33 00:01:37,347 --> 00:01:40,342 The editor will not autosave our program and 34 00:01:40,343 --> 00:01:42,000 you will need to do it manually. 35 00:01:42,580 --> 00:01:44,998 It is a common mistake at the beginner level that 36 00:01:44,999 --> 00:01:48,150 we execute our code without saving the latest changes. 37 00:01:48,840 --> 00:01:52,592 Always make sure that you save the latest version 38 00:01:52,593 --> 00:01:55,700 and then execute your program for correct results. 39 00:01:56,600 --> 00:01:58,458 To compile the program, we will 40 00:01:58,459 --> 00:02:00,576 write commands in the terminal. 41 00:02:00,577 --> 00:02:03,720 The terminal may be made visible from the view menu. 42 00:02:03,721 --> 00:02:05,980 [No audio] 43 00:02:05,981 --> 00:02:08,558 The rustc command, followed by the name of 44 00:02:08,559 --> 00:02:11,560 the file is used to compile the code file. 45 00:02:11,561 --> 00:02:15,950 [No audio] 46 00:02:15,951 --> 00:02:18,052 It will take a few seconds and if 47 00:02:18,053 --> 00:02:20,260 everything is okay, it will return the control 48 00:02:20,261 --> 00:02:23,230 to the terminal without any additional messages. 49 00:02:24,370 --> 00:02:27,188 You may also note that it has generated the 50 00:02:27,189 --> 00:02:30,408 executable file corresponding to our file, which we can 51 00:02:30,409 --> 00:02:32,690 now use to see the program output. 52 00:02:33,430 --> 00:02:35,768 This can be done by writing the command on the 53 00:02:35,769 --> 00:02:41,212 terminal ./Example1, and it will run the application and 54 00:02:41,213 --> 00:02:44,230 will have the appropriate output on the terminal. 55 00:02:45,050 --> 00:02:47,618 This is useful when your program is fairly 56 00:02:47,619 --> 00:02:50,390 simplified and contains only a single file. 57 00:02:51,130 --> 00:02:54,754 However, in real world scenarios, your programs 58 00:02:54,755 --> 00:02:56,966 may tend to be complex, having dependencies 59 00:02:56,967 --> 00:02:58,910 and more than one file. 60 00:02:58,911 --> 00:03:00,752 In that case, you should be 61 00:03:00,753 --> 00:03:03,030 ideally using the Cargo utility. 62 00:03:03,031 --> 00:03:06,038 As pointed out in the previous tutorial, Cargo 63 00:03:06,039 --> 00:03:08,676 is the package manager to install packages and 64 00:03:08,677 --> 00:03:12,116 to track the dependencies and other related stuff. 65 00:03:12,117 --> 00:03:14,612 It is therefore more efficient and effective to use 66 00:03:14,613 --> 00:03:17,870 Cargo for compiling and building our programs in Rust. 67 00:03:18,770 --> 00:03:21,208 Before explaining how to compile and execute the 68 00:03:21,209 --> 00:03:24,072 program using Cargo, I will first delete these 69 00:03:24,073 --> 00:03:26,370 two files that we have just created. 70 00:03:27,110 --> 00:03:30,030 We will first initialize a project using Cargo 71 00:03:30,031 --> 00:03:32,652 using the command of cargo new, and then 72 00:03:32,653 --> 00:03:34,710 the name of the directory or folder. 73 00:03:34,711 --> 00:03:38,660 [No audio] 74 00:03:38,661 --> 00:03:39,942 This will create a new 75 00:03:39,943 --> 00:03:42,580 project in the specified directory. 76 00:03:42,581 --> 00:03:44,198 You may have noticed that we have 77 00:03:44,199 --> 00:03:46,820 a new folder called learning_rust. 78 00:03:47,720 --> 00:03:51,350 When I open this, you may see some files inside it. 79 00:03:52,200 --> 00:03:54,992 One of these files is the Cargo.toml file 80 00:03:54,993 --> 00:03:58,202 which contains information like the name, the version, and 81 00:03:58,203 --> 00:04:00,660 is very similar to the file like pip file 82 00:04:00,661 --> 00:04:03,236 for Python, and JSON for Node. 83 00:04:03,237 --> 00:04:06,526 It contains all the necessary information about our 84 00:04:06,527 --> 00:04:09,998 application and any dependencies that you may have. 85 00:04:09,999 --> 00:04:12,638 By the way, we will cover the dependencies later 86 00:04:12,639 --> 00:04:14,642 on in the course, but for now, just keep 87 00:04:14,643 --> 00:04:17,266 in mind that we will need some information here 88 00:04:17,267 --> 00:04:20,480 when we use other people code and functions. 89 00:04:20,481 --> 00:04:24,812 You may also note a .gitignore file which contains 90 00:04:24,813 --> 00:04:27,782 the location of the folder where our additional files will 91 00:04:27,783 --> 00:04:30,742 go when our code gets compiled, which may not be 92 00:04:30,743 --> 00:04:34,614 directly useful for us, but the compiler generates them. 93 00:04:34,615 --> 00:04:38,202 Finally, we have a source folder, that is src, which 94 00:04:38,203 --> 00:04:42,680 contains all the source files with the .rs extensions. 95 00:04:42,681 --> 00:04:45,226 Inside the source files there is one 96 00:04:45,227 --> 00:04:48,208 rs file which is kind of autogenerated 97 00:04:48,209 --> 00:04:50,932 when we first initialize a project. It contains 98 00:04:50,933 --> 00:04:53,240 the same code that we just saw earlier. 99 00:04:54,060 --> 00:04:56,318 To compile the code in the .rs file, we 100 00:04:56,319 --> 00:04:58,250 will use the command of cargo run. 101 00:04:58,251 --> 00:05:02,100 [No audio] 102 00:05:02,101 --> 00:05:05,290 It will not only compile but also execute the program 103 00:05:05,291 --> 00:05:08,230 and will give us the necessary outputs if any. 104 00:05:09,560 --> 00:05:11,674 Please note that it compiled into the 105 00:05:11,675 --> 00:05:14,096 target folder and then into the Debug. 106 00:05:14,097 --> 00:05:16,318 And right here is the executable file with 107 00:05:16,319 --> 00:05:19,080 the name of learning_rust.exe. 108 00:05:20,220 --> 00:05:22,942 This means that I can run using the 109 00:05:22,943 --> 00:05:27,720 command of ./target/debug/learning_rust. 110 00:05:28,300 --> 00:05:30,530 If you just want to build but not run, 111 00:05:30,531 --> 00:05:33,150 then you will use the command of cargo build. 112 00:05:33,151 --> 00:05:36,890 [No audio] 113 00:05:36,891 --> 00:05:38,188 In this case, it will not 114 00:05:38,189 --> 00:05:40,120 be printing anything on the screen. 115 00:05:40,890 --> 00:05:43,728 If you are interested in building it for production, then 116 00:05:43,729 --> 00:05:47,290 you will use the command of cargo build --release. 117 00:05:47,291 --> 00:05:52,850 [No audio] 118 00:05:52,851 --> 00:05:54,906 It will take some time and afterwards 119 00:05:54,907 --> 00:05:56,378 it will have the message of, Finished 120 00:05:56,379 --> 00:05:59,192 release optimization, which is an indication that 121 00:05:59,193 --> 00:06:01,810 it is now being optimized for production. 122 00:06:02,470 --> 00:06:04,888 Inside the target folder and then inside the 123 00:06:04,889 --> 00:06:08,328 debug there is another folder called release, which 124 00:06:08,329 --> 00:06:11,228 is being created where the optimized version of 125 00:06:11,229 --> 00:06:13,270 the executable has been created. 126 00:06:14,490 --> 00:06:17,138 This executable can now be used for deployment 127 00:06:17,139 --> 00:06:20,242 in a real world scenario. The optimizations 128 00:06:20,243 --> 00:06:22,370 make your Rust code run faster. 129 00:06:22,371 --> 00:06:23,980 However, to optimize the code, it 130 00:06:23,981 --> 00:06:25,770 may take more time to compile. 131 00:06:26,910 --> 00:06:29,654 To summarize, there are two scenarios. 132 00:06:29,655 --> 00:06:31,808 In the first scenario we are in the stage of 133 00:06:31,809 --> 00:06:35,600 development where we would like to rebuild quickly and often, 134 00:06:35,601 --> 00:06:37,776 since we are in the process of writing the code 135 00:06:37,777 --> 00:06:40,160 and we have not yet finalized the code. 136 00:06:40,690 --> 00:06:43,524 In the second scenario, we are done with the code, 137 00:06:43,525 --> 00:06:45,828 and now we would like to finalize the program 138 00:06:45,829 --> 00:06:48,522 that we will be handing over to the user. 139 00:06:48,523 --> 00:06:51,742 So in this case we won't be rebuilding repeatedly. 140 00:06:51,743 --> 00:06:53,288 The objective will be that the 141 00:06:53,289 --> 00:06:55,640 code runs as fast as possible. 142 00:06:55,641 --> 00:06:58,248 In this scenario we will use build using the 143 00:06:58,249 --> 00:07:01,460 release options which will do the optimizations for us. 144 00:07:02,230 --> 00:07:04,220 This is basically all about 145 00:07:04,221 --> 00:07:06,258 the compilation of your programs. 146 00:07:06,259 --> 00:07:08,684 For majority part of the course we will be using the 147 00:07:08,685 --> 00:07:11,772 cargo run and for writing codes we will be generally using 148 00:07:11,773 --> 00:07:15,828 the main function and will write the codes in the main.rs file 149 00:07:15,829 --> 00:07:18,738 which is residing inside the src folder. 150 00:07:18,739 --> 00:07:21,244 That is it for this particular segment of the course. 151 00:07:21,245 --> 00:07:23,314 See you again, and until next tutorial, 152 00:07:23,315 --> 00:07:25,310 enjoy rust programming. 153 00:07:25,311 --> 00:07:32,700 [No audio]