Rustonomicon
180 pages
English
PDF
4.5

Rustonomicon

Rustonomicon

Author:

Year:

Summary:

Introduction

The Rustonomicon is not just a book, but rather a grimoire, a collection of dark knowledge about the Rust programming language. If you have already mastered the basics of Rust and feel ready to look behind the scenes of strict safety guarantees, this book will be your guide to the world of unsafe code, raw pointers, FFI (Foreign Function Interface), and other dangerous but necessary tools. Written by a collective of authors (Various Authors) and maintained by the community, it is the official guide to unsafe Rust, available for free online. The 2022 print edition, 180 pages long, is a compact yet rich reference for those who want to understand how Rust works "under the hood" and how to bypass its safety mechanisms when truly necessary.

What the book is about

The book is dedicated exclusively to the unsafe subset of Rust. It does not teach basic syntax or idiomatic code — it assumes the reader is already proficient in safe Rust. Main topics:

  • Raw pointers (*const T and *mut T): creation, dereferencing, pointer arithmetic.
  • Unsafe functions and traits: declaring and calling unsafe fn, implementing unsafe trait.
  • FFI (Foreign Function Interface): calling C functions from Rust, exporting Rust functions to C, working with extern blocks.
  • Memory management: manual memory management, allocators, creating custom smart pointers.
  • Data representation: alignment, type sizes, struct and enum layouts, #[repr(...)] attributes.
  • Undefined behavior (UB): what you can and cannot do in unsafe code, how to avoid UB.
  • Atomic operations and memory barriers: low-level synchronization.
  • Inline assembly: the asm! macro for platform-dependent code.

Who this book is for

The Rustonomicon is intended for experienced Rust developers who already have experience writing safe code and want to delve into systems programming. It will be useful for:

  • Developers creating libraries with FFI (e.g., wrappers for C/C++ libraries).
  • Engineers working on operating systems, drivers, or embedded systems.
  • Those who want to implement custom data structures with manual memory management (e.g., their own Vec or HashMap).
  • Game engine developers and high-performance application developers where every microsecond matters.
  • Anyone who wants to truly understand how Rust works at the machine code level.

What the reader will learn

After reading the book, you will be able to:

  • Confidently use raw pointers and understand the difference between references and pointers.
  • Write safe wrappers around unsafe code without introducing memory leaks or UB.
  • Interact with C libraries via FFI, passing complex data structures.
  • Create custom allocators and manage memory manually.
  • Understand data layouts in memory, alignment, and type sizes.
  • Use atomic operations for lock-free programming.
  • Avoid undefined behavior and write correct unsafe code.
  • Embed inline assembly for platform-dependent optimizations.

Strengths

The book has several undeniable advantages: