Rust CLI Cheat Sheet

Core rustup and Cargo commands for creating, building, running, and inspecting Rust CLI projects.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Bootstrap and versions
Show rustup version
rustup --version

# Print the rustup version.

Show rustc version
rustc --version

# Print the active Rust compiler version.

Show Cargo version
cargo --version

# Print the active Cargo version.

Update Rust toolchains
rustup update

# Update installed toolchains and rustup itself.

Set stable as default toolchain
rustup default stable

# Use stable globally unless overridden.

Run Cargo with a specific toolchain
cargo +nightly --version

# Use toolchain shorthand for one command.

## Create projects
Create a new binary crate
cargo new hello-cli

# Generate a new package for an executable.

Create a new library crate
cargo new mylib --lib

# Generate a reusable library package.

Initialize Cargo in an existing directory
cargo init

# Create a manifest in the current project folder.

Initialize as library
cargo init --lib

# Create a library package in the current directory.

Locate the manifest
cargo locate-project

# Print the path to the Cargo.toml file Cargo will use.

Inspect package metadata
cargo metadata --format-version 1

# Emit machine-readable package metadata.

## Help and discovery
Show Cargo help
cargo help

# Display top-level help for Cargo.

Show help for a subcommand
cargo help build

# Read options for a specific command.

Find installed tool executable
which cargo

# Show where an executable resolves from.

Show active rustup state
rustup show

# Display installed toolchains, defaults, and active host.

## Manifest basics
Minimal package manifest
[package]
name = "hello-cli"
version = "0.1.0"
edition = "2021"

# Define package metadata and edition.

Add dependencies table
[dependencies]
clap = { version = "4", features = ["derive"] }
anyhow = "1"

# Declare crate dependencies in the manifest.

Add a named binary target
[[bin]]
name = "admin-tool"
path = "src/bin/admin-tool.rs"

# Expose an extra executable from the same package.

Pin minimum Rust version
[package]
rust-version = "1.75"

# Require a minimum supported compiler version.

Recommended next

No recommendations yet.