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.
Project Management 9
cargo new project_namecargo new --lib project_namecargo initcargo buildcargo build --releasecargo runcargo run --releasecargo checkcargo cleanDependency Management 6
cargo add serdecargo add serde --features derivecargo remove serdecargo updatecargo treecargo tree --invertTesting & Docs 5
cargo testcargo test -- --nocapturecargo test test_namecargo doc --opencargo doc --no-depsCode Quality 4
cargo clippycargo clippy --fixcargo fmtcargo fmt -- --checkPublish & Cross-Compile 6
cargo publishcargo publish --dry-runcargo build --target x86_64-unknown-linux-muslrustup target listrustup target add x86_64-pc-windows-gnucargo install cargo-editCore Syntax 14
let s2 = s1;let r = &s;let r = &mut s;fn foo<'a>(x: &'a str) -> &'a strfn foo(x: &str) -> &strmatch x { Some(v) => v, None => 0 }if let Some(v) = opt {}while let Some(v) = iter.next() {}let Ok(v) = res else { return; };Result<T, E>Option<T>fn foo() -> Result<T, E>result?#[derive(Debug, Clone, PartialEq)]Module System 7
use std::collections::HashMap;use std::io::{self, Read};mod my_module;pub fn foo() {}pub(crate) fn internal()pub use crate::utils::helper;crate::module::func()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