Rust Lang

Rust Cargo Commands

Cargo is a lightweight and powerful open-source package manager for the Rust programming language. It is one tool that can take Rust from a relatively normal programming language to one that is powerful and the most delighting with which to work.

Cargo comes with a suite of commands and tools that range from generating a new project to publishing your own packages on the Rust official repository.

Well, Rustaceans, in this article, we will introduce you to the workings of the cargo package manager by learning the most fundamental and useful commands.

Installing Cargo

Before you can use the cargo package manager, you will need to install it. By default, cargo is packaged along with the Rust compiler. Hence, if you have rustc installed, the chances are high that you have cargo installed.

To check if cargo is available in your system, open your terminal and enter the command:

$ cargo --version

cargo 1.58.0 (f01b232bc 2022-01-19)

The cargo –version command will return the version of cargo installed on your system.

Useful Cargo Commands.

The following is a list of some useful commands provided by the cargo package manager:

cargo new

The cargo new command allows you to create a new cargo package in the specified directory.

Example usage of the command is as shown:

$ cargo new [options] path

The command creates a new cargo package with the specified name in the current working directory.

The command support other options such as:

  1. –name name – Specifies the name of the package.
  2. –bin – tells cargo to create a package with the binary target specified. By default, the value is set to src/main.rs
  3. –lib – tells cargo to create a package with the library target as specified. By default, src/lib.rs

cargo init

The cargo init command creates a new cargo manifest in the current working directory. The command is as shown:

cargo init [options] path

It supports similar options as the cargo new command.

cargo install

The cargo install command allows you to build and install a Rust binary. The command syntax is as shown:

cargo build [options] create

You can install a crate from multiple sources such as –git, –path, and –registry. These flags allow you to change the source of the crate.

The command support other options. An example is as shown:

  1. –version – specifies which crate version to install.
  2. –list – lists all the installed packages and their versions.
  3. –force – force overwrite crates and binaries.
  4. –bin – install only the specified binary.
  5. –root – specifies the root directory in which to install the packages.

The above are some options supported by the cargo install command.

cargo uninstall

The cargo uninstall command removes a package installed using the cargo install command.

The command syntax is as shown:

cargo uninstall [options] [spec]

The following are example options to use with the cargo uninstall command.

  1. –package – specifies which package to uninstall.
  2. –bin name – only uninstalls the binary with the specified name.

cargo search

The cargo search command allows you to search for a rust package in the crates.io directory. The command syntax is as shown:

cargo search [options] [query]

The command will search for matching crates on crates.io and returns the found matches along with the description in TOML format.

cargo run

The cargo run command runs the current package. The command syntax is as shown:

cargo run [options] [-- args]

The options after the –args flags are treated as program arguments and are passed to the running binary.

Remember that the cargo run command will run the package in the current working directory. To specify a different directory, you can use the -p flag.

cargo build

The cargo build command compiles the current package and the required dependencies.

The command syntax is as shown:

cargo build [options]

cargo [command] –help

The help command allows you to get the available help menu for a specific command. For example, to get the help for the cargo build command, you can do:

cargo build --help

To get help for the main cargo command, enter the command:

cargo help

cargo tree

The cargo tree command shows a tree format for the dependencies for the specified package.

Command syntax is as:

cargo tree [options]

cargo fix

The cargo fix automatically fixes the linting errors returned by the rust compiler. The command syntax is as shown:

cargo fix [options]

Other Commands

The above are some common commands you will use while working with the cargo package manager. However, there are others that may not pop up as often but can be useful.

These include:

  1. cargo doc – the cargo doc commands allow you to generate the documentation for a package.
  2. cargo fetch – this command fetches the dependencies defined in a package from the network.
  3. cargo clean – removes the generated artifacts.
  4. cargo package – this command accumulates a local package into a distributable tar file.

Closing

The article outlines some common and useful commands when working with the cargo package manager. This list is only a tip of the cargo commands. Check the resource below to learn more about cargo commands.

https://doc.rust-lang.org/cargo/

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list