rgoussu@goussu: ~/library/rust
~/library/rust cat rust-deep-dive.md

Deep dive Rust

# Rust's core model — ownership and borrowing, lifetimes, traits, fearless concurrency, and async — and why the compiler is the point.

Conceptsaved 2026-08-08updated 2026-08-09 #rust#ownership#concurrency#internals

Overview

Rust's proposition is memory safety and data-race freedom without a garbage collector, enforced at compile time by the ownership system. Going deep means internalizing that model — ownership, borrowing, lifetimes — until the borrow checker becomes a design partner rather than an adversary, then layering on traits, Send/Sync, and the async ecosystem. The learning curve is real; the payoff is systems code that is fast and provably free of whole bug classes.

Key points

  • Ownership & moves: every value has one owner; assignment moves by default; RAII (Drop) makes resource cleanup deterministic — memory, files, locks alike.
  • Borrowing: any number of shared &T XOR one exclusive &mut T — aliasing XOR mutation is the single rule behind both memory safety and data-race freedom.
  • Lifetimes: names for "how long a reference is valid"; mostly elided, explicit at API boundaries; the signal to restructure rather than annotate harder.
  • Traits & generics: trait bounds with monomorphization (zero-cost) vs. dyn Trait (dynamic dispatch); Result/Option + ? make errors values, not exceptions.
  • Fearless concurrency: Send/Sync are auto-derived marker traits — the compiler proves what may cross threads; Arc<Mutex<T>>, channels, or scoped threads without fear of races.
  • Async: async fn compiles to state machines polled by an executor (Tokio); no built-in runtime; Pin, cancellation-by-drop, and "async Rust is harder Rust" — worth learning after the core model.
  • Escape hatches: interior mutability (RefCell, Cell, atomics) moves checks to runtime; unsafe narrows trust to audited blocks, it doesn't turn checks off globally.
  • To explore: smart pointers (Box/Rc/Cow), zero-copy parsing, FFI, embedded/no_std, miri for UB detection.

Practice

  • Rustlings (source) — fix-the-compile-error drills that turn borrow-checker fights into muscle memory; the canonical first mile.
  • Exercism Rust track (source) — small mentored problems where idiomatic Result/Option/iterator solutions are the point, not just passing tests.
  • PNGme (source) — a guided build of a PNG chunk encoder/decoder; ownership, byte parsing, and error-as-values on a real binary format.
  • Advent of Code in Rust (source) — a season of puzzles as the vehicle for iterators, lifetimes at API boundaries, and knowing when to clone and when to restructure.
  • Tokio mini-redis (source) — the flagship: an async Redis server that makes Arc<Mutex<T>>, framing, channels, and cancellation-by-drop concrete.

Related