Cargo downloads your Rust project's dependencies and builds your project

Cargo, Rust's Package Manager


The easiest way to get Cargo is to get the Rust nightly build by using the rustup script:

$ curl | sudo bash

This will get you the latest Rust nightly for your platform along with the latest Cargo. You should run this script almost every day to get the latest updates.

If you are on Windows, you can directly download the latest 32bit (Rust and Cargo) or 64bit (Rust and Cargo) nightlies.

Alternatively, you can build Cargo from source.

Let's Get Started

To start a new project with Cargo, use cargo new:

$ cargo new hello_world --bin

We're passing --bin because we're making a binary program: if we were making a library, we'd leave it off.

Let's check out what Cargo has generated for us:

$ cd hello_world
$ tree .
├── Cargo.toml
└── src

1 directory, 2 files

This is all we need to get started. First, let's check out Cargo.toml:


name = "hello_world"
version = "0.0.1"
authors = ["Your Name <>"]

This is called a manifest, and it contains all of the metadata that Cargo needs to compile your project.

Here's what's in src/

fn main() {
    println!("Hello, world!")

Cargo generated a 'hello world' for us. Let's compile it:

$ cargo build
   Compiling hello_world v0.0.1 (file:///path/to/project/hello_world)

And then run it:

$ ./target/hello_world
Hello, world!

We can also use cargo run to compile and then run it, all in one step:

$ cargo run
     Fresh hello_world v0.0.1 (file:///path/to/project/hello_world)
   Running `target/hello_world`
Hello, world!

Going Further

For more details on using Cargo, check out the Cargo Guide