// REF. 09 / RUST

Rust cheat sheet

Ownership and borrowing, String versus &str, Option and Result with the question mark that makes them bearable, match and let-else, structs, traits, closures, iterators, the two collections you use daily, and the cargo commands that run the show. Current to Rust 1.99.1 and the 2024 edition, checked against the Book, std and the Edition Guide in October 2026.

Two habits that pay for themselves: write .expect("why") instead of .unwrap() so the panic explains itself, and run cargo clippy before you argue with the borrow checker by hand.

↓ Download the printable PDF free · letter size · no email needed

Ownership

let s2 = s1;
move: s1 is no longer valid
let s2 = s1.clone();
deep copy — may be expensive
&s
borrow: read-only, any number
&mut s
one mutable borrow at a time
i32, bool, char, f64
Copy types: assigned, not moved

Strings

&str
borrowed slice; literals are &str
String::from("hi")
owned, growable, UTF-8
s.push_str("x")
append a &str; push('c') for a char
s1 + &s2
moves s1, borrows s2
format!("{a}-{b}")
build a String, borrows everything

Option & result

Option<T>
Some(v) or None
Result<T, E>
Ok(v) or Err(e)
f()?
unwrap, or return the error early
.unwrap_or(0)
value or default
.ok_or(err)
Option to Result
.map(f) / .and_then(f)
transform inside / chain fallible
.expect("why")
panic with context — over unwrap

Patterns

match x { .. }
exhaustive; arms are pattern => expr
Some(i) => Some(i + 1),
bind the inner value
other => use(other),
catch-all that binds
_ => (),
catch-all, do nothing
if let Some(v) = opt {}
one pattern, ignore the rest
let Some(v) = opt else {
else must return, break or panic

Structs & traits

struct P { x: i32 }
named fields; whole instance is mut
P { x, ..other }
field shorthand, struct update
fn area(&self) -> u32
method; &mut self to mutate
#[derive(Debug, Clone)]
auto-impl traits; print with {:?}
impl Trait for Type {}
implement a trait
fn f(x: &impl Trait)
trait-bound parameter
fn f<T: A + B>(x: &T)
generic with bounds; or where

Closures & iters

|x| x + 1
closure; types inferred
move || use(list)
take ownership, e.g. for threads
Fn / FnMut / FnOnce
reads / mutates / consumes captures
.iter() / .into_iter()
borrow items / take ownership
.map(f).filter(p)
lazy until consumed
.collect::<Vec<_>>()
consume into a collection

Collections

vec![1, 2, 3]
Vec literal; Vec::new() for empty
&v[2] / v.get(2)
panics / Option<&T>
for x in &mut v {}
mutate in place via *x
HashMap::new()
use std::collections::HashMap
m.entry(k).or_insert(0)
insert if absent; returns &mut V

Cargo

cargo new name
new package; init for an existing dir
cargo build --release
optimized build
cargo run
build and run
cargo check
type-check, skip codegen
cargo test
run the tests
cargo add serde
add a dependency to Cargo.toml
cargo clippy / cargo fmt
lint / format
cargo doc --open
build docs, open in browser

Edition 2024

edition = "2024"
Cargo.toml; stable since 1.85
cargo fix --edition
migrate, then bump the edition
unsafe extern "C" {}
extern blocks must say unsafe
#[unsafe(no_mangle)]
unsafe attributes
r#gen
gen is reserved now
Rust Field Manual Desk Mat

// KEEP IT UNDER YOUR KEYBOARD

Rust Field Manual Desk Mat

from $34 · printed to order in the USA

See the desk mat