DEV Community

Cover image for Top 10 Rust Cargo Commands
David Adewoyin
David Adewoyin

Posted on • Updated on

Top 10 Rust Cargo Commands

Cargo is the Rust package manager, and is a powerful tool that can make programming in Rust quite a delight.It provides various commands that can be used in your daily programming. The list of commands that Cargo supports can be extended through Rust crates written by the community.

The array of commands makes Cargo a versatile tool worth knowing for any Rust developer.

This article provides a list of helpful Cargo commands worth knowing and using.

Top Cargo Commands to Know

1. cargo install [options] crate... :

cargo install is used for building and installing a Rust binary. There are multiple sources from which a crate can be installed. The default location is crates.io but the --git , --path, and --registry flags can be set to change the source.
Example of using cargo install is cargo install sqlx which install the sqlx crate from crates.io.

2. cargo uninstall [options] [spec...]:

As the name suggest this command removes a package installed with cargo-install.

3. cargo tree [options]:

cargo tree will display a tree of dependencies to the terminal. An example of a simple project that depends on the "rand" package:

myproject v0.1.0 (/myproject)
└── rand v0.7.3
    ├── getrandom v0.1.14
    │   ├── cfg-if v0.1.10
    │   └── libc v0.2.68
    ├── libc v0.2.68 (*)
    ├── rand_chacha v0.2.2
    │   ├── ppv-lite86 v0.2.6
    │   └── rand_core v0.5.1
    │       └── getrandom v0.1.14 (*)
    └── rand_core v0.5.1 (*)
[build-dependencies]
└── cc v1.0.50
Enter fullscreen mode Exit fullscreen mode

4. cargo search [options] [query...]:

This performs a textual search for crates on crates.io. The matching crates will be displayed along with their description in TOML format suitable for copying into a Cargo.toml manifest.
Example of using cargo search is : cargo search serde which returns a list of crate with textual search marching serde and a description which include their version number.

serde = "1.0.130"                         # A generic serialization/deserialization framework
discord_typed_interactions = "0.1.0"      # suppose you're working with discord slash commands and you want statically typed requ…
serde_json_experimental = "1.0.29-rc1"    # A JSON serialization file format
alt_serde_json = "1.0.61"                 # A JSON serialization file format
serde_json = "1.0.70"                     # A JSON serialization file format
serde_partiql = "1.1.65"                  # A PartiQL data model serialization file format
cargo-geiger = "0.11.1"                   # Detects usage of unsafe Rust in a Rust crate and its dependencies.
serde-encrypt = "0.6.0"                   # Encrypts all the Serialize
serde-encrypt-core = "0.6.0"              # Encrypts all the Serialize
typescript-definitions = "0.1.10"         # serde support for exporting Typescript definitions
... and 2787 crates more (use --limit N to see more)
Enter fullscreen mode Exit fullscreen mode

5. cargo edit :

This tool extends Cargo to allow you to add, remove, and upgrade dependencies by modifying your Cargo.toml file from the command line.

Currently available sub-command includes:

  • cargo add
  • cargo rm
  • cargo upgrade
  • cargo set-version

To use cargo-edit, you will need to install it first through cargo install cargo-edit as it's a cargo extension.

6. cargo +nightly udeps :

cargo-udeps is used to find unused dependencies in Cargo.toml.
While compilation of this tool also works on Rust stable, it needs Rust nightly to actually run. cargo-udeps is a cargo extension so you have to install it first before it can be used.
It can be installed through cargo install cargo-udeps --locked

7. cargo expand :

Once installed, cargo expand prints out the result of macro expansion and #[derive] expansion applied to the current crate.It is a wrapper around the more verbose compiler command:cargo rustc --profile=check -- -Zunpretty=expanded.

Install with cargo install cargo-expand.

Example of using cargo-expand

 #[derive(Debug)]
struct S;

fn main() {
    println!("{:?}", S);
}
Enter fullscreen mode Exit fullscreen mode

cargo expand

#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
struct S;
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::fmt::Debug for S {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {
            S => {
                let mut debug_trait_builder = f.debug_tuple("S");
                debug_trait_builder.finish()
            }
        }
    }
}
fn main() {
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(
            &["", "\n"],
            &match (&S,) {
                (arg0,) => [::core::fmt::ArgumentV1::new(arg0, ::core::fmt::Debug::fmt)],
            },
        ));
    };
}
Enter fullscreen mode Exit fullscreen mode

8. cargo tarpaulin :

Tarpaulin is a code coverage reporting tool for the Cargo build system.

It's installed with cargo install cargo-tarpaulin. Currently tarpaulin only works on x86_64 CPU architectures running Linux.Use cargo tarpaulin --ignore-tests to compute code coverage for your application while ignoring test functions.

9. cargo audit :

cargo audit can be installed through cargo install cargo-audit and is used to audit Cargo.lock files for crates with security vulnerabilities reported to the RustSec Advisory Database.

10. cargo deny:

cargo-deny is a cargo plugin that lets you lint your project's dependency graph to ensure all your dependencies conform to your expectations and requirements.
It's used to audit Cargo.lock files for crates with security vulnerabilities, limit the usage of particular dependencies, their licenses, sources to download from, detect multiple versions of same packages in the dependency tree and more.
use cargo install --locked cargo-deny && cargo deny init && cargo deny check to installs cargo-deny, initializes your project with a default configuration, then runs all of the checks against your project.
Example of a check is the licenses check that is used to verify that every crate you use has license terms you find acceptable .

cargo deny check licenses.
License check of rust crate through cargo-deny
To learn more on how to use cargo-deny check the cargo deny book.

The list of Cargo commands is vast and this article provides a small sample of them that you can use and hopefully learn how powerful Cargo is.

Top comments (0)