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

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" ]

[[bin]]

name = "hello-world" # the name of the executable to generate

Next, add src/hello-world.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" ]

[[bin]]

name = "hello-world" # the name of the executable to generate

[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 hello-world.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 hello-world.rs and recompile, Cargo will intelligently skip recompiling color, which is still fresh.

$ touch src/hello-world.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/hello-world.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