Learning Cargo – The build tool for Rust

27 Nov, 2018
Xebia Background Header Wave

Cargo is the build tool for the Rust programming language. It downloads, compiles and tests your project. More importantly, Cargo manages your project dependencies. Cargo has very good documentation and is very accessible. Cargo het been written in Rust and will be the most important tool in your Rust toolchain! Lets take a closer look.

Installing Cargo

Cargo is automatically installed with Rust. On a Mac, Rust can be installed by typing:

$ brew install rust

$ cargo --version
cargo 1.29.0 (524a578d7 2018-08-05)

Common Commands

Cargo provides commands what you would expect from a build tool like ‘update’, ‘test’, ‘build’ and ‘run’.

$ cargo

    build       Compile the current project
    check       Analyze the current project and report errors, but don't build object files
    clean       Remove the target directory
    doc         Build this project's and its dependencies' documentation
    new         Create a new cargo project
    init        Create a new cargo project in an existing directory
    run         Build and execute src/
    test        Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this project to the registry
    install     Install a Rust binary
    uninstall   Uninstall a Rust binary

Basic Workflow

Cargo has a basic workflow like creating a new project, testing code, running the project, and building the project to a static binary.

$ pwd

# creating a new project
$ cargo new hello-rust
Created binary (application) `hello-rust` project
cd hello-rust

# testing
$ cargo test
Compiling hello-rust v0.1.0 (file:///Users/dennis/projects/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.15s
Running target/debug/deps/hello_rust-0b720b8081218362

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

# running
$ cargo run
Compiling hello-rust v0.1.0 (file:///Users/dennis/projects/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 2.47s
Running `target/debug/hello-rust`
Hello, world!

# building
$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.37s

# runnning
$ ./target/debug/hello-rust
Hello, world!


Cargo.toml describes your project and the dependencies. The file is edited by us, the developer. To add dependencies to the project, add new entries in the package section.

name = "hello-rust"
version = "0.1.0"
authors = ["dennis <>"]


Dependency Management

Cargo provides the central package registry that serves as a location to discover and download packages. Cargo is configured to use it by default to find requested packages.
Lets add the rusoto dependency to out project. Rusoto is the AWS SDK for Rust.

name = "hello-rust"
version = "0.1.0"
authors = ["dennis <>"]

rusoto_core = "0.35.0"
rusoto_s3 = "0.35.0"

Listing the buckets in our account

Lets edit the file src/

extern crate rusoto_core;
extern crate rusoto_s3;

use rusoto_core::Region;
use rusoto_s3::{S3Client, S3};

fn main() {
    let client = S3Client::new(Region::EuWest1);
    match client.list_buckets().sync() {
        Ok(output) => {
            match output.buckets {
                Some(s3_bucket_lists) => {

                    for bucket in s3_bucket_lists {
                        println!("Name: {}, CreationDate: {}",, bucket.creation_date.unwrap_or_default());
                None => println!("No buckets in account!"),
        Err(error) => {
            println!("Error: {:?}", error);


Cargo is the build and dependency management tool for the Rust programming language. Cargo provides build, test and dependency management functionalities for Rust projects. Cargo uses the central package registry. Cargo uses the Cargo.toml file to model the project and the dependencies. Cargo is very easy to use and when you have used Maven, Gradle, SBT or Go before, Cargo should be very familiar.


Get in touch with us to learn more about the subject and related solutions

Explore related posts