This tutorial explains frequently used commands of the Cargo Package Manager:

  • cargo init
  • cargo build
  • cargo test
  • cargo publish
  • cargo run
  • cargo doc
  • cargo clean to clean the artifacts

How to Create a New Rust Project

The cargo new command is used to create a new application.

cargo new rustapp
 Created binary (application) `rustapp` package

It creates a new application folder named rustapp, which contains Cargo.toml to store metadata such as name, version, dependencies sections, and profiles.

You can also use cargo new --vcs none to create a new project without any version control system configuration.

How to Install Crate Binaries from crates.io

The cargo install command is used to install binaries from crates.io.

cargo install package

Here, package is a crate name library from crates.io.

Cargo Release Profiles

Release profiles are profiles used to store pre-created and custom configurations with more configuration options to compile code in Rust. They are used with the cargo build command.

Cargo has two profiles: release and dev.

The release profile is used to release packages, while dev is the default profile used for development.

cargo build uses the default profile dev. To build a release profile, use the --release option.

cargo build
cargo build --release

Publish a Package

First, log in to crates.io website and obtain a token.

cargo login token

Next, run the cargo publish command to publish to crates.io.

cargo publish