Leanpub Header

Skip to main content

Rust Deep Dives

how the compiler works

What the compiler is really doing

Minimum price

$5.00

$20.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Most Rust books teach you the language — ownership, traits, the borrow checker, how to structure a crate. Rust Deep Dive assumes you've already been through that. It picks up where those books stop and asks a different question: what does rustc actually do with the code you write?

A generic function isn't one function — it's N functions, one per concrete type, each compiled and optimized separately. Option<&T> isn't a tagged union with wasted space — the compiler notices that references are never null and reuses the invalid bit pattern, so the Option costs nothing. An async fn isn't sugar over a callback — it's a state machine, generated by the compiler, with a field for every value that has to survive across an .await point. None of this is visible in the source code. All of it explains why the code behaves, compiles, and performs the way it does.

Each chapter takes one piece of the compiler's behavior and goes deep: what internal representation it uses, what tool output (--emit=mir--emit=llvm-ircargo expandcargo bloat) actually shows you, and what tradeoffs the compiler is making on your behalf. Across 35 chapters and eleven parts, the book follows the compiler's own pipeline and then radiates out into the corners that pipeline touches:

  • Compilation itself — parsing, monomorphization, const generics, trait resolution, lang items, and both macro systems
  • The borrow checker — NLL, lifetime inference, variance, and where unsafe takes over what the compiler can no longer check
  • Memory layout — repr variants, niche optimization, smart pointers, and the allocator
  • Dynamic dispatch — closures as compiler-generated structs, vtables, and trait objects
  • Async/await — the state machine transform, executors, and wakers
  • Ownership's edge cases — drop order, move semantics, and interior mutability
  • LLVM and codegen — IR, optimization passes, and SIMD
  • Concurrency — Send/Sync, atomics and memory ordering, and Rayon
  • The standard library's internals — no_std, string representation, collections, and iterators
  • FFI and unsafe boundaries, and the build system — linking and binary size

By the end, the goal isn't trivia about rustc internals. It's a mental model precise enough that borrow checker errors stop feeling arbitrary, performance regressions stop feeling mysterious, and reaching for unsafePin, or a #[repr] attribute feels like a deliberate choice rather than a leap of faith.

Who this book is for

This book is for developers who already write Rust — comfortably, not necessarily expertly — and have started asking why. You'll get the most out of it if you:

  • Have written and shipped real Rust code — a library, a service, a CLI, anything past toy examples
  • Understand ownership and borrowing well enough to fix a borrow checker error without guessing
  • Are comfortable reading unsafe blocks, even if you don't write them often
  • Have some curiosity about compilers, systems programming, or performance

You don't need to be a compiler engineer to read this book, and you won't come out of it one — but you'll never look at a cargo build the same way again.

Part I: The Compilation Pipeline

  • From Source to Binary
  • Monomorphization
  • Const Generics
  • Trait Resolution and the Type System Machinery
  • Bounds — Traits, Lifetimes, +, and where
  • Lang Items
  • Declarative Macros
  • Procedural Macros

Part II: The Borrow Checker

  • Non-Lexical Lifetimes and the Borrow Checker Algorithm
  • References, Reborrowing, and Lifetime Elision
  • Unsafe Rust and What the Compiler Can't Check

Part III: Memory Layout

  • Type Layout
  • Smart Pointers Under the Hood
  • The Allocator Story

Part IV: Closures, Traits

  • Closure Implementation
  • Trait Objects and Virtual Dispatch

Part V: Async/Await — The State Machine

  • How Async/Await Compiles
  • The Executor and Waker Machinery
  • Async Performance and Pitfalls

Part VI: The Ownership

  • Drop Order, Drop Glue, and Destructors
  • Move Semantics
  • Interior Mutability

Part VII: LLVM

  • What rustc Asks LLVM to Do
  • Optimization Passes and What They Catch
  • SIMD and Target Features

Part VIII: Concurrency

  • Send, Sync, and the Concurrency Model
  • Atomics and Memory Ordering in Rust
  • Rayon and Data Parallelism

Part IX: The Standard Library's

  • no_std — Core, Alloc, and the Shape of the Standard Library
  • String Representation and UTF-8 Internals
  • Collections Internals
  • Iterator Internals and Optimization

Part X: FFI and Unsafe

  • FFI — Calling C from Rust and Rust from C
  • Writing Sound Unsafe Code

Part XI: Build System

  • Cargo, Linking, and Binary Size

Appendices

  • Compiler Flags Reference
  • Reading Compiler Output
  • The Rust Reference and RFC Index

Share this book

Categories

Author

About the Author

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub