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 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:
The command creates a new cargo package with the specified name in the current working directory.
The command support other options such as:
- –name name – Specifies the name of the package.
- –bin – tells cargo to create a package with the binary target specified. By default, the value is set to src/main.rs
- –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:
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:
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:
- –version – specifies which crate version to install.
- –list – lists all the installed packages and their versions.
- –force – force overwrite crates and binaries.
- –bin – install only the specified binary.
- –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:
The following are example options to use with the cargo uninstall command.
- –package – specifies which package to uninstall.
- –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:
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:
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 [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:
To get help for the main cargo command, enter the command:
cargo tree
The cargo tree command shows a tree format for the dependencies for the specified package.
Command syntax is as:
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:
- cargo doc – the cargo doc commands allow you to generate the documentation for a package.
- cargo fetch – this command fetches the dependencies defined in a package from the network.
- cargo clean – removes the generated artifacts.
- 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.