Using Cargo
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 00:07] So real world Rust projects generally won't use Rust C directly. Instead, we can use cargo to build and run our program.
[00:08 - 00:20] Cargo is similar to MPM. It manages your dependencies, project info, and gives access to scripts for doing various things with your code. So let's go ahead and create a new project using cargo.
[00:21 - 00:34] So we'll change back into the parent directory, close out of VS code here. And we can do that with the cargo new command and then just pass it the name of the project.
[00:35 - 00:39] And we'll call that hello2. You can see that it creates a binary application by default.
[00:40 - 00:52] And that just means that it will, the output will be a binary that we can execute, as opposed to a library that's consumed by other people's code. Go ahead and change directory into that.
[00:53 - 01:11] Open it up. And so right off the bat, you can see that cargo has created a couple of files for us. We have this cargo.tomble file, which is the equivalent to the package.json in the Node JavaScript world.
[01:12 - 01:48] It also creates a git-ignore file because cargo new by default creates a new git repo project. And you can see that it's ignoring the target repo or target directory , which is where the build artifacts and the output of our compile step will end up being. We also have this source directory, which is, like I mentioned, where you keep your source files in a REST project. And we have the main.rs file, where you can see that we have our main function with the hello world program already here by default.
[01:49 - 02:05] So in order to run a REST program using cargo, we can just go ahead and run cargo run. And you can see that first does a compile step up here before running the program, and then we get our output.
[02:06 - 02:18] If you just wanted to compile the program and didn't want to run it afterwards, then you would use cargo build. And that just does the build step on its own without running.
[02:19 - 02:29] You can see here that we now have the target directory is populated with some of the build artifacts. So in our debug directory, our hello to program is right here.
[02:30 - 02:54] And we could actually, if we wanted to run that program manually just by doing slash target debug and then hello to, and you can see that we get the same output there. So we'll go over some more advanced cargo commands later in further modules, but these are all the basics that you need to get started using cargo for a new REST project.
So real world Rust projects generally won't use Rust C directly. Instead, we can use cargo to build and run our program. Cargo is similar to MPM. It manages your dependencies, project info, and gives access to scripts for doing various things with your code. So let's go ahead and create a new project using cargo. So we'll change back into the parent directory, close out of VS code here. And we can do that with the cargo new command and then just pass it the name of the project. And we'll call that hello2. You can see that it creates a binary application by default. And that just means that it will, the output will be a binary that we can execute, as opposed to a library that's consumed by other people's code. Go ahead and change directory into that. Open it up. And so right off the bat, you can see that cargo has created a couple of files for us. We have this cargo.tomble file, which is the equivalent to the package.json in the Node JavaScript world. It also creates a git-ignore file because cargo new by default creates a new git repo project. And you can see that it's ignoring the target repo or target directory , which is where the build artifacts and the output of our compile step will end up being. We also have this source directory, which is, like I mentioned, where you keep your source files in a REST project. And we have the main.rs file, where you can see that we have our main function with the hello world program already here by default. So in order to run a REST program using cargo, we can just go ahead and run cargo run. And you can see that first does a compile step up here before running the program, and then we get our output. If you just wanted to compile the program and didn't want to run it afterwards, then you would use cargo build. And that just does the build step on its own without running. You can see here that we now have the target directory is populated with some of the build artifacts. So in our debug directory, our hello to program is right here. And we could actually, if we wanted to run that program manually just by doing slash target debug and then hello to, and you can see that we get the same output there. So we'll go over some more advanced cargo commands later in further modules, but these are all the basics that you need to get started using cargo for a new REST project.