Cargo (Rust Package Manager) Cheatsheet
Tags: rust, beginners, tooling, todayilearned
Published: 2021-01-26 06:04:52

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
```

Windows:

Manually install <a href="https://forge.rust-lang.org/infra/other-installation-methods.html">here</a>.

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

Compiling from source:

```
git clone https://github.com/rust-lang/cargo
```

Note: This installs just cargo

Checking the installation process:

```
cargo --version
```

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

****

## General Usage

```
cargo --version
```

Gives you the current version.

```
cargo --help [COMMAND]
```

Shows help message for the command specified.

```
cargo
```

Shows useful commands as well as a help message.

```
cargo --list
```

Lists all the available commands.

```
cargo --verbose
```

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

```
cargo --quiet
```

Disable output.

```
cargo --locked or cargo --frozen
```

Requires `Cargo.lock` to be up to date.

```
cargo --color WHEN
```

Enables colors: always, auto, or never.

```
cargo --offline
```

Prevents cargo from accessing the network.

```
cargo -z
```

Unstable, nightly-only flags to cargo.

```
cargo +toolchain
```

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

****

## Creating a new Rust project

```
cargo new --bin NAME
```

Creates a new Rust project with the given name.

```
cargo new --lib NAME
```

Creates a new Rust library with the given name.

```
cargo init --bin
```

Creates a new Rust project in your current directory.

```
cargo init --lib
```

Creates a new Rust library in your current directory.

****

## Using Cargo in your projects

```
cargo build or cargo b
```

Builds your Rust project.

```
cargo run or cargo r
```

Executes your Rust project.

```
cargo bench
```

Executes the benchmarks of your Rust project.

```
cargo test or cargo t
```

Executes the tests of your Rust project.

```
cargo check or cargo c
```

Checks for errors in your Rust project.

```
cargo doc
```

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

```
cargo clean
```

Removes the `target` directory.

****

## Using crates

```
cargo search
```

Searches for crates on `crates.io`.

```
cargo install CRATE
```

Installs the specified crate.

```
cargo install --list
```

Lists all the crates you installed.

****

## Pushlishing your crate

```
cargo login
```

Logs in to your `crates.io` account.

```
cargo owner
```

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

```
cargo publish
``` 

Publishes your crate to `crates.io`.

```
cargo yank
```

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**

<a href="https://buttondown.email/hb">📰 Newsletter</a>
<a href="https://github.com/henryboisdequin">🐱 GitHub</a>
<a href="https://twitter.com/henryboisdequin">🐦 Twitter</a>