reference · rust fluency
cargo cheatsheet
daily commands
| command | what it does |
|---|---|
cargo new name | scaffold a new binary project |
cargo run | compile and run (debug build) |
cargo check | fast compile analysis, no binary. use while iterating |
cargo build | compile to target/debug/ |
cargo build --release | optimized compile to target/release/ |
cargo test | run all tests |
cargo add dep_name | add a dependency to Cargo.toml |
cargo fmt | format code (rustfmt) |
cargo clippy | linter. catches idiomatic mistakes |
cargo doc --open | build and open docs for your crate and deps |
project layout
myproject/
├── Cargo.toml # manifest: metadata + dependencies
├── Cargo.lock # pinned dep versions. commit it for binaries
└── src/
├── main.rs # binary entry point (fn main)
└── lib.rs # library entry point, if the crate is a library
a crate can be a binary, a library, or both. one package may contain multiple crates.
Cargo.toml essentials
[package]
name = "myproject"
version = "0.1.0"
edition = "2024"
[dependencies]
serde = "1.0" # semver-compatible: >=1.0.0, <2.0.0
edition is the language edition, not your version. 2021 and 2024 are the ones you will see in the wild.
mental model
- cargo = pip + venv + setuptools + make, in one tool
- crates.io = pypi/npm registry
cargo checkfor feedback,cargo build --releasefor shipping
source: the cargo book and the rust book ch 1.3.