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

Installing

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

$ curl www.rust-lang.org/rustup.sh | sudo bash

This will get you the latest nightly build for your platform along with the latest Cargo. You can run this every day to get the latest updates.

Let’s Get Started

To start, add a Cargo.toml to the root of your project. Here’s a simple one to get you started.

[package]

name = "hello-world"
version = "0.1.0"
authors = [ "wycats@example.com" ]

Next, add src/main.rs to your project.

fn main() {
    println!("Hello world!");
}

And compile it:

$ cargo build
   Compiling hello-world v0.1.0
$ ./target/hello-world
Hello world!

Depend on a Library

To depend on a library, add it to your Cargo.toml.

[package]

name = "hello-world"
version = "0.1.0"
authors = [ "wycats@example.com" ]

[dependencies.color]

git = "https://github.com/bjz/color-rs.git"

You added the color library, which provides simple conversions between different color types.

Now, you can pull in that library using extern crate in main.rs.

extern crate color;

use color::{RGB, ToHSV};

fn main() {
    println!("Converting RGB to HSV!");
    let red = RGB::new(255u8, 0, 0);
    println!("HSV: {}", red.to_hsv::<f32>());
}

Compile it:

$ cargo build
    Updating git repository `https://github.com/bjz/color-rs.git`
   Compiling color v1.0.0 (https://github.com/bjz/color-rs.git)
   Compiling hello-world v0.1.0
$ ./target/hello-world
Converting RGB to HSV!
HSV: HSV { h: 0, s: 1, v: 1 }

Recompiling

If you modify main.rs and recompile, Cargo will intelligently skip recompiling color, which is still fresh.

$ touch src/main.rs
$ cargo build
       Fresh color v1.0.0 (https://github.com/bjz/color-rs.git)
   Compiling hello-world v0.1.0

To update the repository from Github, pass the --update-remotes (or -u) flag to cargo build.

$ touch src/main.rs
$ cargo build -u
    Updating git repository `https://github.com/bjz/color-rs.git`
   Compiling color v1.0.0 (https://github.com/bjz/color-rs.git)
   Compiling hello-world v0.1.0