Rust Concepts
Advanced Rust from the type system to unsafe code, async and production, measured on Rust 1.100 and sourced from the official docs, books and papers, one concept at a time.
Threads & Atomics
Send, Sync and Interior Mutability
Send and Sync as auto traits, with a compiled table of which std types implement them, the real E0277 errors for Rc, RefCell and Cell across threads, a measured refcount corruption from a false unsafe impl Send on Rc, UnsafeCell as the only legal door to interior mutability and the assembly it changes, Cell and RefCell sizes and the RefCell re-entrancy panic, OnceLock and LazyLock (1.80) racing, poisoning and re-entrant deadlock, and hand-written unsafe impl Send and Sync with the edition 2021 disjoint-capture gotcha, on Rust 1.100.0.
Atomics and Memory Ordering
Rust's memory model, inherited from C++20, measured on arm64, with the instruction each ordering compiles to (ldapr for Acquire, ldar for SeqCst, ldadd, casal, dmb), store buffering litmus tests where Release/Acquire still shows the forbidden outcome tens of thousands of times per 10 million rounds while SeqCst or a SeqCst fence shows zero, message passing where Relaxed fails rarely and Release/Acquire or fences never do, lost updates from a load followed by a store, spurious failures of compare_exchange_weak only on LL/SC builds, and update/try_update (1.95) with fetch_update deprecated in 1.99, on Rust 1.100.0.
Async
Futures, Wakers and the Poll Model
The Future trait with Poll, Context and Waker, the enum state machine the compiler generates for an async fn (measured with size_of_val and -Zprint-type-sizes), why future size grows with what is held across .await and where Box::pin helps, a minimal std-only block_on executor built on Wake and thread park, and the silent hang of a Pending without a wake, including how it disguises itself as latency under tokio::time::timeout.
Pin and Self-Referential Futures
Why an async fn state machine can point into itself (measured in memory), the Pin guarantee and the Unpin auto trait, the real compiler errors when polling an unpinned future, pin! versus Box::pin, structural pinning and projection with pin-project-lite (including its Drop ban and PinnedDrop), and what happens natively and under Miri when unsafe code moves a pinned future, with the RFC 2349 rationale.
Tokio Runtime Internals
The current_thread and multi_thread schedulers and the drivers a hand-built Builder leaves off, worker local queues, the global queue and work stealing measured with tokio_unstable steal counters, the LIFO slot measured on a ping-pong, the 128-unit cooperative budget with consume_budget and yield_now, the mio I/O driver over kqueue and epoll and the millisecond timer wheel, and a measured 300 ms stall from std::thread::sleep inside a task fixed with spawn_blocking or block_in_place, on tokio 1.54.1.
Async Traits and Async Closures
async fn in traits and RPITIT since 1.75, the future cannot be sent between threads safely error when spawning generically and its fixes with an explicit -> impl Future + Send desugaring or trait_variant::make, return type notation still unstable in 1.100, why traits with async methods are not dyn compatible and what boxing with async-trait or Pin<Box<dyn Future>> costs in allocations per call, and async closures with AsyncFn, AsyncFnMut and AsyncFnOnce (1.85) versus closures returning futures, including the Send gap of AsyncFn.
Cancellation Safety and Graceful Shutdown
Dropping a future as cancellation, what tokio::select! does to the losing branches, JoinHandle detach versus abort, the Tokio cancel-safety lists, a measured data loss with read_exact in a select! loop fixed with a persistent buffer and read_buf, JoinSet, CancellationToken and child tokens, and a graceful shutdown with signal handling, broadcast cancellation, TaskTracker and a deadline, including the SIGTERM that ctrl_c alone does not catch.