DEV Community

loading...

Cargo (Rust Package Manager) Cheatsheet

hb profile image Henry Boisdequin ・2 min read

This is a practical cheat sheet to use when using the Rust package manager Cargo. A package manager allows you to install dependencies, update dependencies, modify your project, configure your project, and more. The best package manager for Rust is Cargo which was built by the Rust core team. Let's get right into the cheatsheet!


Installing and updating Cargo

Mac/Linux:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

Windows:

Manually install here.

Note: This installs the whole Rust toolchain including cargo, rustup, and Rust itself.

Compiling from source:

git clone https://github.com/rust-lang/cargo
Enter fullscreen mode Exit fullscreen mode

Note: This installs just cargo

Checking the installation process:

cargo --version
Enter fullscreen mode Exit fullscreen mode

If this command executes, tells you cargo's version, without errors then cargo is successfully installed.


General Usage

cargo --version
Enter fullscreen mode Exit fullscreen mode

Gives you the current version.

cargo --help [COMMAND]
Enter fullscreen mode Exit fullscreen mode

Shows help message for the command specified.

cargo
Enter fullscreen mode Exit fullscreen mode

Shows useful commands as well as a help message.

cargo --list
Enter fullscreen mode Exit fullscreen mode

Lists all the available commands.

cargo --verbose
Enter fullscreen mode Exit fullscreen mode

Enables verbose output. Use cargo -vv for more verbose output.

cargo --quiet
Enter fullscreen mode Exit fullscreen mode

Disable output.

cargo --locked or cargo --frozen
Enter fullscreen mode Exit fullscreen mode

Requires Cargo.lock to be up to date.

cargo --color WHEN
Enter fullscreen mode Exit fullscreen mode

Enables colors: always, auto, or never.

cargo --offline
Enter fullscreen mode Exit fullscreen mode

Prevents cargo from accessing the network.

cargo -z
Enter fullscreen mode Exit fullscreen mode

Unstable, nightly-only flags to cargo.

cargo +toolchain
Enter fullscreen mode Exit fullscreen mode

Overrides current toolchain with the toolchain specified (example: cargo +nightly switches to the nightly toolchain).


Creating a new Rust project

cargo new --bin NAME
Enter fullscreen mode Exit fullscreen mode

Creates a new Rust project with the given name.

cargo new --lib NAME
Enter fullscreen mode Exit fullscreen mode

Creates a new Rust library with the given name.

cargo init --bin
Enter fullscreen mode Exit fullscreen mode

Creates a new Rust project in your current directory.

cargo init --lib
Enter fullscreen mode Exit fullscreen mode

Creates a new Rust library in your current directory.


Using Cargo in your projects

cargo build or cargo b
Enter fullscreen mode Exit fullscreen mode

Builds your Rust project.

cargo run or cargo r
Enter fullscreen mode Exit fullscreen mode

Executes your Rust project.

cargo bench
Enter fullscreen mode Exit fullscreen mode

Executes the benchmarks of your Rust project.

cargo test or cargo t
Enter fullscreen mode Exit fullscreen mode

Executes the tests of your Rust project.

cargo check or cargo c
Enter fullscreen mode Exit fullscreen mode

Checks for errors in your Rust project.

cargo doc
Enter fullscreen mode Exit fullscreen mode

Creates your Rust project documentation (use cargo doc --open to open it).

cargo clean
Enter fullscreen mode Exit fullscreen mode

Removes the target directory.


Using crates

cargo search
Enter fullscreen mode Exit fullscreen mode

Searches for crates on crates.io.

cargo install CRATE
Enter fullscreen mode Exit fullscreen mode

Installs the specified crate.

cargo install --list
Enter fullscreen mode Exit fullscreen mode

Lists all the crates you installed.


Pushlishing your crate

cargo login
Enter fullscreen mode Exit fullscreen mode

Logs in to your crates.io account.

cargo owner
Enter fullscreen mode Exit fullscreen mode

Manages owners of your crate (add owners by using the --add flag).

cargo publish
Enter fullscreen mode Exit fullscreen mode

Publishes your crate to crates.io.

cargo yank
Enter fullscreen mode Exit fullscreen mode

Removes your crate from crates.io.


This is a practical cheatsheet on Cargo! I hope you learned a new command or more about Cargo. If you would like to access this cheatsheet, later on, I would recommend bookmarking this page or adding it to your reading list. Thanks for reading!

Henry

📰 Newsletter
🐱 GitHub
🐦 Twitter

Discussion (0)

pic
Editor guide