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

Install, inspect, and verify the Rust toolchain.

Show rustup version

Print the rustup version.

bashANYrustupversion
bash
rustup --version
Notes

Confirms rustup is installed and shows its current version.

Show rustc version

Print the active Rust compiler version.

bashANYrustcversion
bash
rustc --version
Notes

Shows which `rustc` is currently active in your shell.

Show Cargo version

Print the active Cargo version.

bashANYcargoversion
bash
cargo --version
Notes

Useful for debugging toolchain mismatches and environment issues.

Update Rust toolchains

Update installed toolchains and rustup itself.

bashANYrustupupdate
bash
rustup update
Notes

Updates channels like stable, beta, and nightly when installed.

Set stable as default toolchain

Use stable globally unless overridden.

bashANYrustuptoolchainstable
bash
rustup default stable
Notes

Sets the default toolchain used outside directories with overrides.

Run Cargo with a specific toolchain

Use toolchain shorthand for one command.

bashANYcargorustuptoolchain
bash
cargo +nightly --version
Notes

Cargo supports `+toolchain` shorthand when installed via rustup.

Create projects

Create new Rust binary and library packages.

Create a new binary crate

Generate a new package for an executable.

bashANYcargonewbinary
bash
cargo new hello-cli
Notes

`cargo new` creates a new package with a manifest and starter source.

Create a new library crate

Generate a reusable library package.

bashANYcargonewlibrary
bash
cargo new mylib --lib
Notes

Use `--lib` when the package is meant to be a library crate.

Initialize Cargo in an existing directory

Create a manifest in the current project folder.

bashANYcargoinit
bash
cargo init
Notes

Useful when you already created the folder or are converting existing code.

Initialize as library

Create a library package in the current directory.

bashANYcargoinitlibrary
bash
cargo init --lib
Notes

Creates `Cargo.toml` and a `src/lib.rs` starter file.

Locate the manifest

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

bashANYcargomanifestproject
bash
cargo locate-project
Notes

Helpful when working in nested directories or workspaces.

Inspect package metadata

Emit machine-readable package metadata.

bashANYcargometadatajson
bash
cargo metadata --format-version 1
Notes

Frequently used by tooling and scripts to inspect crates and workspaces.

Help and discovery

Explore Cargo commands and built-in help.

Show Cargo help

Display top-level help for Cargo.

bashANYcargohelp
bash
cargo help
Notes

Lists commands and global options.

Show help for a subcommand

Read options for a specific command.

bashANYcargohelpbuild
bash
cargo help build
Notes

Equivalent to the command-specific manual page for build.

Find installed tool executable

Show where an executable resolves from.

bashANYcargopathshell
bash
which cargo
Notes

Useful when multiple toolchain installs exist on the machine.

Show active rustup state

Display installed toolchains, defaults, and active host.

bashANYrustuptoolchainsshow
bash
rustup show
Notes

Shows the active toolchain and installed targets/components.

Manifest basics

Common `Cargo.toml` structures for CLI apps.

Minimal package manifest

Define package metadata and edition.

tomlANYcargomanifesttoml
toml
[package]
name = "hello-cli"
version = "0.1.0"
edition = "2021"
Notes

`Cargo.toml` is the package manifest written in TOML.

Add dependencies table

Declare crate dependencies in the manifest.

tomlANYcargodependenciestoml
toml
[dependencies]
clap = { version = "4", features = ["derive"] }
anyhow = "1"
Notes

Dependencies are normally declared in the `[dependencies]` table.

Add a named binary target

Expose an extra executable from the same package.

tomlANYcargobinmanifest
toml
[[bin]]
name = "admin-tool"
path = "src/bin/admin-tool.rs"
Notes

Binary packages can expose additional executables with `[[bin]]` targets.

Pin minimum Rust version

Require a minimum supported compiler version.

tomlANYcargomanifestmsrv
toml
[package]
rust-version = "1.75"
Notes

The manifest supports declaring a minimum supported Rust version.

Recommended next

No recommendations yet.