Rust Cheat Sheet
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A single-page, colour-coded summary of the Rust language, written against Rust 1.99.0 and the 2024 edition (checked
2026-10-06) — the rustup and cargo command map and a Cargo.toml skeleton with features, profiles, [lints] and a
workspace, primitive types, literals and casts, let/mut/const/static and shadowing, control flow with loop values,
labels, if let, let-else and let chains, the ownership and borrowing rules in one box with the lifetime elision
rules, structs, enums and pattern syntax, Option/Result combinators and ? with an error-type recipe, collection,
string and iterator one-liners, generics versus impl Trait versus dyn Trait, closures and the Fn traits, a
smart-pointer and interior-mutability chooser, the Send/Sync table, threads, channels and async, modules and
visibility, macro_rules! fragment specifiers, the unsafe superpowers, testing and tooling flags, and a strip of what
changed since older tutorials — handy for quick memorization or as a
downloadable PDF desk reference.
For the full explanation, every option, and additional examples, see the dedicated pages:
Start here — Rust Reference.
Getting started — Getting Started, Toolchain, rustup & Editions and Syntax, Comments & Style.
Core language — Variables, Mutability & Constants, Types & Values, Functions & Expressions and Control Flow.
Ownership — Ownership & Moves, Borrowing & References, Slices & Dynamically Sized Types and Lifetimes.
Data and errors — Structs & Methods, Enums & Pattern Matching, Option, Result & Combinators, Error Handling, Collections and Strings & Text.
Abstraction — Generics & Const Generics, Traits, Trait Objects & dyn Compatibility, Any & Downcasting, Closures & Fn Traits, Iterators, Type Conversions & Coercions and Standard Traits & Operator Overloading.
Memory — Smart Pointers, Interior Mutability & Lazy Initialization, Drop, RAII & Drop Check and Memory Layout & repr.
Concurrency and async — Threads & Message Passing, Shared State, Send/Sync & Atomics, Data Parallelism with Rayon, Async/Await & Futures, Pin & Unpin and Async Runtimes & Tokio.
Code organization and tooling — Modules, Crates & Visibility, Cargo & Workspaces, Testing & Benchmarking, Documentation with rustdoc, Tooling: Clippy, rustfmt & rust-analyzer and Debugging & Diagnostics.
Advanced and interop — Macros, Unsafe Rust, FFI & C Interop, Python Interop with PyO3 and WebAssembly.
Worked example
The example parses two numbers with ?, handles the error case in match, and chains iterator adapters.
use std::num::ParseIntError;
fn total_pages(a: &str, b: &str) -> Result<u32, ParseIntError> {
let x: u32 = a.trim().parse()?;
let y: u32 = b.trim().parse()?;
Ok(x + y)
}
fn main() {
println!("{:?}", total_pages("120", " 80 "));
match total_pages("120", "abc") {
Ok(n) => println!("{n} pages"),
Err(e) => println!("error: {e}"),
}
let titles = vec!["Dune", "Emma", "Ulysses"];
let long: Vec<usize> = titles.iter().map(|t| t.len()).filter(|&n| n > 4).collect();
println!("{long:?}");
}
Ok(200)
error: invalid digit found in string
[7]
Each ? returns the Err early, converting it with From; the iterator chain is lazy and runs only when collect() drives it.
References
The PDF summarizes the official documentation; each source is cited in full on the page listed above that covers it, and in the section bibliography.