1 00:00:06,570 --> 00:00:07,740 - [Instructor] Okay, so in this section, 2 00:00:07,740 --> 00:00:10,050 we are going to see how to use Cargo 3 00:00:10,050 --> 00:00:13,350 to create a new Rust application. 4 00:00:13,350 --> 00:00:17,490 Cargo is part of the Rust toolset. 5 00:00:17,490 --> 00:00:20,550 In practice, you will always use Cargo 6 00:00:20,550 --> 00:00:23,430 whenever you are creating a Rust application 7 00:00:23,430 --> 00:00:25,800 or building it, or running it, okay? 8 00:00:25,800 --> 00:00:29,670 So, up until now I've been writing a handcrafted Rust file 9 00:00:29,670 --> 00:00:31,980 and compiling it using the Rust compiler. 10 00:00:31,980 --> 00:00:33,510 You can do that, 11 00:00:33,510 --> 00:00:36,090 but it's better to use Cargo for everything. 12 00:00:36,090 --> 00:00:39,480 So, it's the kind of one-stop shop 13 00:00:39,480 --> 00:00:42,047 for everything that you want to do in Rust. 14 00:00:42,047 --> 00:00:44,818 It will generate an application initially, 15 00:00:44,818 --> 00:00:48,510 it'll give you a consistent kind of directory structure 16 00:00:48,510 --> 00:00:51,570 and it nudges you towards best practice, okay? 17 00:00:51,570 --> 00:00:54,930 So it's basically the tool you should always be using 18 00:00:54,930 --> 00:00:56,640 whenever you are building, 19 00:00:56,640 --> 00:00:59,430 or creating, or running a Rust application. 20 00:00:59,430 --> 00:01:00,360 So, like I said, 21 00:01:00,360 --> 00:01:03,180 it's included as part of the standard Rust toolset. 22 00:01:03,180 --> 00:01:05,250 So if you've installed the Rust compiler, 23 00:01:05,250 --> 00:01:07,770 you've also installed Cargo already. 24 00:01:07,770 --> 00:01:09,270 This is how you can use Cargo 25 00:01:09,270 --> 00:01:11,100 to generate the new application. 26 00:01:11,100 --> 00:01:12,750 You say "cargo new" 27 00:01:12,750 --> 00:01:14,490 and then you give it the name of the application. 28 00:01:14,490 --> 00:01:17,550 Rust prefers your application to use lowercase 29 00:01:17,550 --> 00:01:18,660 and underscores. 30 00:01:18,660 --> 00:01:22,145 This is called snake case and this is what Rust prefers. 31 00:01:22,145 --> 00:01:23,760 So I'm going to do that. 32 00:01:23,760 --> 00:01:25,740 I've already actually done this in the demo package, 33 00:01:25,740 --> 00:01:27,330 let me show you. 34 00:01:27,330 --> 00:01:30,450 Here's the demo folder for the current package 35 00:01:30,450 --> 00:01:31,827 for the current part of the demo. 36 00:01:31,827 --> 00:01:34,380 rustdev\lesson01_getting_started. 37 00:01:34,380 --> 00:01:35,760 And you'll notice 38 00:01:35,760 --> 00:01:37,960 that I've already got hello_world_via_cargo. 39 00:01:39,660 --> 00:01:41,640 Okay, so we could look at that 40 00:01:41,640 --> 00:01:43,380 or we could generate a new application 41 00:01:43,380 --> 00:01:45,030 which is what I'm going to do instead. 42 00:01:45,030 --> 00:01:46,653 cargo new, 43 00:01:47,790 --> 00:01:51,090 and hello_world_via_cargo_2. 44 00:01:51,090 --> 00:01:52,990 That's going to be my new application. 45 00:01:54,570 --> 00:01:56,700 Okay, so that was quick. 46 00:01:56,700 --> 00:01:57,570 And what you'll find, 47 00:01:57,570 --> 00:02:00,930 it's generated a new folder that contains that application. 48 00:02:00,930 --> 00:02:03,540 So I now have a new folder, 49 00:02:03,540 --> 00:02:05,073 hello_world_via_cargo_2. 50 00:02:07,459 --> 00:02:10,980 So, I'm gonna change into that directory, 51 00:02:10,980 --> 00:02:14,820 and let's see what we've got generated. 52 00:02:14,820 --> 00:02:17,760 So, it generates, 53 00:02:17,760 --> 00:02:18,780 well, it actually generates, 54 00:02:18,780 --> 00:02:19,613 you can't see it here, 55 00:02:19,613 --> 00:02:22,830 but it's generated a Git repository folder 56 00:02:22,830 --> 00:02:24,450 so that you can upload your application 57 00:02:24,450 --> 00:02:27,120 to a Git repository, or Bitbucket, 58 00:02:27,120 --> 00:02:28,899 and a .gitignore file 59 00:02:28,899 --> 00:02:33,330 specifying what files not to upload to Git. 60 00:02:33,330 --> 00:02:36,390 All the source code is in the source folder. 61 00:02:36,390 --> 00:02:39,930 And there's also a package file called Cargo.toml 62 00:02:39,930 --> 00:02:42,930 which we're gonna have a look at in a moment. 63 00:02:42,930 --> 00:02:44,490 Okay, so just a confirmation. 64 00:02:44,490 --> 00:02:46,020 This is what it actually looks like. 65 00:02:46,020 --> 00:02:50,190 There is a Git folder, which is the Git repository, 66 00:02:50,190 --> 00:02:52,290 the source folder, which contains our code 67 00:02:53,310 --> 00:02:54,660 a .gitignore file, 68 00:02:54,660 --> 00:02:56,130 and then a toml file 69 00:02:56,130 --> 00:02:59,580 which contains basically information about our application, 70 00:02:59,580 --> 00:03:01,350 what dependencies it has, 71 00:03:01,350 --> 00:03:03,873 what version number it is and suchlike. 72 00:03:04,740 --> 00:03:07,590 Okay, so this is the packaging file, 73 00:03:07,590 --> 00:03:10,110 containing metadata about our package 74 00:03:10,110 --> 00:03:12,000 and the list of any other libraries 75 00:03:12,000 --> 00:03:15,000 or any other dependencies our application needs. 76 00:03:15,000 --> 00:03:18,090 The Rust library is fairly complete 77 00:03:18,090 --> 00:03:20,250 but there were third-party packages, 78 00:03:20,250 --> 00:03:21,570 third-party libraries, 79 00:03:21,570 --> 00:03:23,670 that you might also need to access. 80 00:03:23,670 --> 00:03:26,010 For example, if you're talking to a database, 81 00:03:26,010 --> 00:03:28,380 you'll need to download some app packages 82 00:03:28,380 --> 00:03:30,870 that have database capabilities. 83 00:03:30,870 --> 00:03:33,780 So we'll have a look at how to add additional dependencies 84 00:03:33,780 --> 00:03:35,730 into your application later. 85 00:03:35,730 --> 00:03:39,146 At the moment, this is what the toml file looks like. 86 00:03:39,146 --> 00:03:41,760 Whatever the name of the package is, the application, 87 00:03:41,760 --> 00:03:44,060 so for us it would be hello_world_via_cargo_2, 88 00:03:45,330 --> 00:03:48,150 a version number, with kind of like a release number 89 00:03:48,150 --> 00:03:50,310 and a major version and a minor version, 90 00:03:50,310 --> 00:03:52,050 that's up to you to manage. 91 00:03:52,050 --> 00:03:52,883 That's your number. 92 00:03:52,883 --> 00:03:54,630 You can change this as you like. 93 00:03:54,630 --> 00:03:57,750 And then the addition of the Rust compiler that you're using 94 00:03:57,750 --> 00:04:00,530 as generated by the Cargo tool. 95 00:04:00,530 --> 00:04:04,320 So that's information about your package, your application. 96 00:04:04,320 --> 00:04:05,640 And then this section here, 97 00:04:05,640 --> 00:04:07,710 dependencies is empty at the moment, 98 00:04:07,710 --> 00:04:10,470 but later on we'll be adding entries in here 99 00:04:10,470 --> 00:04:12,960 which give us access to, 100 00:04:12,960 --> 00:04:15,060 for example there's a library called Chrono 101 00:04:15,060 --> 00:04:17,460 for time manipulation. 102 00:04:17,460 --> 00:04:21,270 There's a library called MySQL to access the MySQL database 103 00:04:21,270 --> 00:04:22,103 and so on. 104 00:04:22,103 --> 00:04:24,420 Okay, so later on in the video series, 105 00:04:24,420 --> 00:04:26,550 we'll be adding items underneath here 106 00:04:26,550 --> 00:04:29,853 to download libraries that our application needs. 107 00:04:30,840 --> 00:04:32,940 Okay, so I'm gonna open my application, 108 00:04:32,940 --> 00:04:34,470 hello_world_via_cargo_2. 109 00:04:34,470 --> 00:04:36,770 I'm gonna open this one in Visual Studio Code. 110 00:04:43,530 --> 00:04:45,060 And here it is. 111 00:04:45,060 --> 00:04:46,653 You see there's the toml file. 112 00:04:47,610 --> 00:04:49,050 And, as described, 113 00:04:49,050 --> 00:04:50,433 and here's the source file. 114 00:04:51,600 --> 00:04:52,470 And in the source file, 115 00:04:52,470 --> 00:04:55,800 we just have a single main.rs. 116 00:04:55,800 --> 00:04:57,510 Okay, as we would expect. 117 00:04:57,510 --> 00:04:59,460 So, we're kind of at the point now 118 00:04:59,460 --> 00:05:02,412 where we've generated the application using Cargo, 119 00:05:02,412 --> 00:05:04,260 it has the source code, 120 00:05:04,260 --> 00:05:05,790 obviously we can add other source files 121 00:05:05,790 --> 00:05:07,470 in here if we wanted to. 122 00:05:07,470 --> 00:05:09,120 And it has the toml file, 123 00:05:09,120 --> 00:05:11,310 which we could edit if we wanted to, 124 00:05:11,310 --> 00:05:12,840 and then we could use Cargo 125 00:05:12,840 --> 00:05:16,350 to build the application and to run the application. 126 00:05:16,350 --> 00:05:17,520 Okay, and that's what we're going to see 127 00:05:17,520 --> 00:05:18,543 in the next section.