Cargo
8 minutes of reading
In our previous chapter, we compiled our hello world program using rustc. Just compiling with rustc is fine for small programs, but as your project grows, you'll want to be able to manage your project conveniently and make it easy to share your code with other people. Here's where Cargo shines.
Cargo is a Rust project manager, it takes care of things like building your code, downloading and building the libraries that your code depends on, testing and many other things
As the vast majority of Rust projects use Cargo, we will assume that you're using it for the rest of the course. Cargo comes installed with Rust itself so it should have already been installed if you follow the installation guide in the Setup chapter.
Run the following command in your terminal to check if Cargo is installed, if it outputs a version number then it has been installed already.
$ cargo --version
Now, we will use Cargo to build and run the Hello World program we just wrote in the Hello World chapter.
The following command creates an executable application within the hello-world directory. Executables are binary executable files often called just binaries.
$ cargo new hello-world --bin
$ cd hello-world
If we look into the hello-world directory, we can see that Cargo has generated two files and one directory for us: a Cargo.toml and a src directory with a main.rs file inside. It has also initialized a new git repository along with a .gitignore file.
Open up Cargo.toml. The section starting with the line [package] are statements foor configuring a package. The last line [dependencies] is the start of a section for you to list any crates (packages of Rust code) that your project will depend on so that Cargo knows to download and compile those. We won't need any crates for this Hello World project therefore below it it's empty.
1 2 3 4 5 6
[package] name = "hello-world" version = "0.1.0" edition = "2021" [dependencies]
Now let's look at src/main.rs. Cargo has generated a simple “Hello World!” program for you!
1 2 3
fn main() { println!("Hello, world!"); }
Now, run the following command to compile your program.
$ cargo build
This creates an executable file in target/debug/hello_cargo (or target\debug\hello_cargo.exe on Windows), which you can run with this command:
$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
Hello, world!
Running cargo build for the first time also causes Cargo to create a new file at the top level called Cargo.lock. Cargo uses Cargo.lock to keep track of the exact versions of dependencies used to build your project. You won't ever need to touch this file yourself; Cargo will manage its contents for you.
Alternatively, if you want to run the program directly, we can also use cargo run to compile and then run it immediately.
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/hello_cargo`
Hello, world!
Notice that this time, we didn't see the output telling us that Cargo was compiling hello_cargo. Cargo figured out that the files hadn't changed, so it just ran the binary. If you had modified your source code, Cargo would have rebuilt the project before running it, and you would have seen output like this
$ cargo run
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.33 secs
Running `target/debug/hello_cargo`
Hello, world!
Finally, let's introduce the cargo check. This will quickly check your code to make sure that it compiles, but not bother producing an executable. It's useful when you want to quickly check if your code compiles, that's because running cargo check is much faster than running cargo build.
$ cargo check
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs
When your project is finally ready for release, you can use cargo build --release to compile your project with optimizations. This will create an executable in target/release instead of target/debug. These optimizations make your Rust code run faster, but turning them on makes your program take longer to compile.
That's all you need to get started on using Cargo! You have now learnt how to build and compile Rust programs using this convenient tool, Whoopee!!!