Hands-on Rust reference covering syntax, types, methods, and idioms.
This project helps you quickly recall basic Rust methods and idiomatic approaches without searching through documentation or making AI queries.
When learning a new language, you often forget available methods and standard patterns — this cheat sheet provides quick reference examples.
Examples focus on demonstrating individual methods rather than complex logic. This reduces cognitive load and lets you concentrate on language concepts.
rust-cheat-sheet/
├── src/
│ ├── main.rs # Entry point — runs all examples
│ ├── lib.rs # Library root — exports modules
│ │
│ ├── basics/ # Basic types and operations
│ │ ├── options.rs
│ │ ├── results.rs
│ │ ├── strings.rs
│ │ ├── numbers.rs
│ │ ├── arrays_slices.rs
│ │ ├── documentation.rs
│ │ ├── functions.rs
│ │ ├── conditionals.rs
│ │ ├── loops.rs
│ │ └── closures.rs
│ │
│ ├── collections/ # Collection types
│ │ ├── vecs.rs
│ │ ├── hashmaps.rs
│ │ └── iterators.rs
│ │
│ ├── error_handling/ # Error handling
│ │ ├── question_mark.rs
│ │ ├── from_trait.rs
│ │ ├── custom_error.rs
│ │ └── box_dyn_error.rs
│ │
│ ├── macros/ # Macros
│ │ ├── declarative.rs
│ │ ├── built_in.rs
│ │ ├── formatting.rs
│ │ └── advanced.rs
│ │
│ ├── advanced/ # Advanced features
│ │ ├── smart_pointers.rs
│ │ └── traits.rs
│ │
│ ├── std_lib/ # Standard library
│ │ ├── time.rs
│ │ └── fs.rs
│ │
│ ├── ownership/ # Ownership system
│ │ ├── move_semantics.rs
│ │ ├── borrowing.rs
│ │ └── lifetimes.rs
│ │
│ └── types/ # Custom types
│ ├── structs.rs
│ ├── enums.rs
│ ├── pattern_matching.rs
│ ├── custom_traits.rs
│ └── generics.rs
│
├── Cargo.toml
├── flake.nix
└── README.md
The primary way to use this cheat sheet is reading the source code. Each module contains self-contained examples with documentation:
# Open a specific module in your editor
nvim src/basics/options.rs
zeditor src/collections/vecs.rscargo runcargo build
cargo build --releasecargo testcargo doc --openThe best way to remember is to repeat the examples yourself:
- Copy an example to your own project
- Try different input values
- Combine multiple methods together
- Apply to your real-world code
Active practice beats passive reading.
This project includes a Nix flake for reproducible development environment:
nix develop- Rust 1.85+ (edition 2024)
- Nix (optional, for flake-based development)
Each module contains:
- Standalone example functions demonstrating single methods
- Module-level documentation (
//!) - Function documentation (
///) - Unit tests in
#[cfg(test)]blocks - Doc tests where applicable
- Option, Result — error handling types
- String, &str — text operations
- Numbers — arithmetic, overflow handling
- Arrays, Slices — fixed and dynamic views
- Functions — declaration, closures
- Conditionals — if, match, if let
- Loops — loop, while, for
- Closures — syntax, capture modes, Fn traits
- Vec — dynamic arrays
- HashMap — hash tables
- Iterators — lazy sequences
- Question mark — error propagation with
? - From trait — automatic error conversion
- Custom errors — defining error types
- Box — type-erased errors
- Declarative —
macro_rules!, patterns, repetition - Built-in —
println!,format!,dbg!,assert!,vec! - Formatting — format specifiers, alignment, precision, numeric formats
- Advanced — TT muncher, code generation, hygiene
- Move semantics — ownership transfer
- Borrowing — references without ownership
- Lifetimes — reference validity
- Structs — custom data types
- Enums — sum types with variants
- Pattern matching — destructuring syntax
- Traits — shared behavior
- Generics — type parameters, trait bounds, where clause
- Smart pointers — Box, Rc, Arc, RefCell
- Standard traits — Clone, Default, From/Into
- Time — Duration, Instant, SystemTime
- Filesystem — file and directory operations
MIT