rust/quick-reference
NORMAL rustc 1.96.0

01Hello World

Entry point is fn main(). println! is a macro (note the !) — it expands at compile time, it's not a regular function.

02Variables & Mutability

Bindings are immutable by default. Add mut to allow change. Shadowing lets you reuse a name — even with a new type.

03Data Types

Scalars, fixed compound types, and the two string kinds: &str (borrowed slice) vs String (heap-owned, growable).

04Control Flow

if is an expression. loop can return a value via break. Ranges: .. exclusive, ..= inclusive.

05Ownership

Every value has one owner; when the owner leaves scope the value is dropped. Assignment moves non-Copy types. Primitives implement Copy.

06Borrowing & References

References let you use a value without taking ownership. The borrow checker enforces: many shared &T XOR one mutable &mut T — never both at once.

07Structs & Enums

Structs group named fields; enums are tagged unions (variants can carry data). impl blocks attach methods. Self = the type itself.

08Pattern Matching

match is exhaustive and total. Option<T> replaces null — you must handle the missing case.

09Error Handling

No exceptions, no nulls. Failures are Result<T, E>. The ? operator short-circuits and propagates Err upward.

10Traits & Generics

Traits are interfaces. Generics + trait bounds monomorphize at compile time — zero-cost abstraction, no vtable unless you use dyn.

11Cargo

Cargo is the build system + package manager. Cargo.toml declares deps; src/main.rs is the binary entry.

12Modules & Imports

Code is organised with mod. use brings paths into scope; pub makes items visible outside their module. Paths are absolute from the crate root or relative to the current module.

13Closures & Iterators

Closures capture their environment; annotate with Fn (by ref), FnMut (by mut ref), FnOnce (by value). Iterators are lazy — nothing runs until you collect or consume.

14Lifetimes

A lifetime tells the compiler how long a reference is valid. You rarely write them — elision infers the common cases. Write them when a function returns a borrow tied to an input.

15Popular Crates

Add with cargo add <name>. These are the load-bearing dependencies in most real Rust codebases.

  • serde serialization
  • tokio async runtime
  • anyhow app errors
  • thiserror library errors
  • clap CLI parsing
  • reqwest HTTP client
  • rand randomness
  • rayon data parallelism
  • itertools iterator extras
  • tracing structured logging
  • chrono dates & times
  • sqlx async SQL
NORMAL
file: rust-quick-ref.html
section: 01/15
ln: 1
rust 1.96.0