Registries

Cargo CLI

Cargo is a package manager and can compile using rustc. Rust packages are called “crates”.

$ cargo -V
cargo 1.32.0-beta (1b6702f22 2018-12-06)

Some common commands:

$ cargo update
$ cargo build
$ cargo run
$ cargo test
$ cargo doc

The commands in the section below were done from within the my_package directory, if you want to see the result.

Create

See Rust Project Template for a complete example.

Syntax

To create a crate at a given path:

$ # Create binary. (Default)
$ cargo new PATH # --bin

$ # Create library.
$ cargo new PATH --lib

Or, initialize an existing directory.

$ mkdir my_dir
$ cd my_dir
$ cargo --init

Using new initializes with a .git directory and .gitignore file, while --init does not.

Example

Create a new package, which is a Git repo with an ignore file. The implied default --bin option is to create a binary package with a hello world binary executable.

$ cargo new my_package
     Created binary (application) `my_package` package
$ cd my_package

Structure of the new my_package directory:

.git/
src/main.rs
Cargo.toml
.gitignore

The .gitignore excludes the target directory (used for output) and *.rs.bk files.

Build

Use check subcommand:

$ # Check a local package and all of its dependencies for errors
$ cargo check
   Compiling my_package v0.1.0 (.../my_package)
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s

Use build subcommand:

$ # Compile a local package and all of its dependencies
$ cargo build
   Compiling my_package v0.1.0 (.../my_package)
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s

To see the rustc command (and the arguments it gets from your *.toml file), clear the output directory and build from scratch, but with the verbose argument.

$ rm -rf target
$ cargo build --verbose
Compiling my_package v0.1.0 (.../my_package)
     Running `rustc --edition=2018 --crate-name my_package src/main.rs ... ...`
    Finished dev [unoptimized + debuginfo] target(s) in 0.68s

Run

Build and run a package:

$ cargo run
   Compiling my_package v0.1.0 (.../my_package)
    Finished dev [unoptimized + debuginfo] target(s) in 3.63s
     Running `target/debug/my_package`
Hello, world!

Install a binary package

From docs.

$ cargo install CRATE
$ cargo install --git URL CRATE

Update packages

  • cargo-upgrades.

    Shows which dependencies in Cargo.toml can be upgraded to a newer version.

    It’s similar to cargo-outdated, but has a simpler implementation, so it won’t complain about path dependencies or potential version conflicts. Simply checks whether there is a newer (stable) version for each dependency.

Install cargo-upgrades

$ cargo install -f cargo-upgrades

Usage

In a Rust/Cargo project:

$ cargo upgrades

Outdated packages

  • cargo-outdated

    Cargo subcommand for displaying when dependencies are out of date

Install cargo-outdated

$ cargo install cargo-outdated

Usage

$ cargo outdated
Name             Project  Compat  Latest   Kind         Platform
----             -------  ------  ------   ----         --------
clap             2.20.0   2.20.5  2.26.0   Normal       ---
clap->bitflags   0.7.0    ---     0.9.1    Normal       ---
...