Rust Cheatsheet - Rust & Cargo Command Reference

This reference is for Rust developers — whether you are still wrestling with the borrow checker or shipping a production crate — and centers on Cargo, the toolchain you touch every day. It covers scaffolding projects, building (debug vs release), running binaries, testing, generating docs, running Clippy lints, formatting with rustfmt, and cross-compiling for a target. Unlike a flat list of cargo subcommands, entries follow the workflow you actually repeat: edit → check/build → test → lint → fmt → release. After reading, you should be able to take a fresh crate from cargo new through a clean Clippy + test pass to a release build.

Languages·51 commands·Last updated 2026-07-21
rustcargoProgramming

Project Management 9

cargo new project_name
Create a new binary project
cargo new --lib project_name
Create a library project
cargo init
Initialize project in current dir
cargo build
Build (debug mode)
cargo build --release
Build release (optimized)
cargo run
Build and run
cargo run --release
Run in release mode
cargo check
Quick check (no binary)
cargo clean
Clean the target dir

Dependency Management 6

cargo add serde
Add a dependency
cargo add serde --features derive
Add dep with a feature
cargo remove serde
Remove a dependency
cargo update
Update Cargo.lock
cargo tree
View dependency tree
cargo tree --invert
Invert: who depends on a crate

Testing & Docs 5

cargo test
Run all tests
cargo test -- --nocapture
Show println! output
cargo test test_name
Run a named test
cargo doc --open
Build and open docs
cargo doc --no-deps
Docs for this crate only

Code Quality 4

cargo clippy
Run Clippy lints
cargo clippy --fix
Auto-fix Clippy warnings
cargo fmt
Format code
cargo fmt -- --check
Check formatting (CI)

Publish & Cross-Compile 6

cargo publish
Publish to crates.io
cargo publish --dry-run
Dry-run publish
cargo build --target x86_64-unknown-linux-musl
Cross-compile to Linux
rustup target list
List available targets
rustup target add x86_64-pc-windows-gnu
Add a Windows target
cargo install cargo-edit
Install a global command

Core Syntax 14

let s2 = s1;
Ownership transfer (move)
let r = &s;
Immutable borrow
let r = &mut s;
Mutable borrow
fn foo<'a>(x: &'a str) -> &'a str
Explicit lifetime annotation
fn foo(x: &str) -> &str
Lifetime elision rules
match x { Some(v) => v, None => 0 }
match pattern matching
if let Some(v) = opt {}
if let shorthand
while let Some(v) = iter.next() {}
while let loop match
let Ok(v) = res else { return; };
let-else (else returns)
Result<T, E>
Result type (Ok or Err)
Option<T>
Option type (Some/None)
fn foo() -> Result<T, E>
Return Result for errors
result?
Error propagation operator (?)
#[derive(Debug, Clone, PartialEq)]
Auto-derive trait impls

Module System 7

use std::collections::HashMap;
Import a module
use std::io::{self, Read};
Import several items (incl. self)
mod my_module;
Declare a submodule (file)
pub fn foo() {}
Public visibility
pub(crate) fn internal()
Crate-visible only
pub use crate::utils::helper;
Re-export
crate::module::func()
Absolute path reference

Tips

  • cargo check is much faster than cargo build - use it daily for compile checks.
  • cargo clippy is the official linter - add it to CI.
  • Always run cargo publish --dry-run before publishing.
  • At any time you may have one mutable reference OR several immutable ones, never both.
  • The ? operator works only in functions returning Result; it propagates Err upward.
  • Lifetime elision covers common cases; complex ones need explicit lifetime params.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us