The C to Rust Migration Book | MainmatterSkip to main content Mainmatter
The C to Rust Migration Book<br>By Mainmatter<br>Overview<br>The course<br>Table of contents<br>About the author<br>About Mainmatter
Migrate from C to Rust without breaking the world<br>Securing and modernizing software is a top priority today. Tech debt and memory-safety bugs remain the largest sources of vulnerabilities, which is why teams increasingly turn to Rust for its reliability and memory safety. But rewrites are hard, scary, and carry a lot of risk. Mainmatter assembled our experience from helping clients successfully modernize some of the worlds most important software into this book.<br>Whether you rewrite a self-contained library from scratch or incrementally migrate a large, complex codebase that keeps changing, this book teaches you the techniques to succeed. You will learn how to maintain a safe mixed C-Rust codebase, design and structure clean APIs, and translate common C idioms into idiomatic Rust.
The C to Rust Migration Book<br>Start learning
This course is built around our philosophy of self-paced exercises. Each concept is paired with an exercise you solve in a real Rust package, with a test suite that checks your solution.<br>It assumes prior knowledge of Rust and some C. No prior FFI experience required. That's what the course teaches.<br>By the end you'll be ready to introduce Rust into a C codebase in a productive way.
Sign up for updates<br>New chapters are on the way. Subscribe to our newsletter to be the first to find out.<br>Your e-mail (Required) Required<br>Subscribe
Table of contents<br>1Foreign Function Interfaces<br>The fundamentals of FFI: how C and Rust communicate at the binary level through the C ABI, symbol tables, and the linker. You'll write your first extern "C" blocks by hand and see what you're promising the compiler when you declare a binding, and what goes wrong when the signature doesn't match.
2Types and Data Across the Boundary<br>How to move primitive values, strings, structs, and collections across the language boundary while keeping ownership clear. You'll use bindgen to generate Rust bindings from C headers, and cheadergen, Mainmatter's tool for the reverse direction, to generate C headers from your Rust code, and configure both for non-trivial types.
3FFI-Safe Types<br>Raw pointers and untyped primitives are a major source of bugs at the FFI boundary. You'll learn safer alternatives: NonNull, Option, newtype wrappers, and enum discriminants that carry meaning instead of bare values, and how to design types that make invalid states unrepresentable even when talking to C code that has no type safety of its own.
4FFI validation<br>Every FFI function is a trust boundary. C callers can pass null pointers, out-of-range values, or invalid UTF-8, and Rust can't catch that at compile time. You'll build the discipline of validating and narrowing inputs at the boundary, so invalid data from C never reaches safe Rust or your business logic.
5Error Handling and API Design<br>Rust's Result and ? stop at extern "C". You'll convert Rust error types into C-compatible results with error-code enums and out-parameters, and surface failure information without leaking Rust internals. You'll also learn to expose coarse operations rather than per-field setters and keep your API surface small.
6Production-Quality FFI<br>You'll document FFI functions with Rust's /// # Safety convention and numbered invariants, so every unsafe contract is explicit and surfaces as C doc comments in the generated headers via cheadergen. We cover build integration, automated checks, and treating your FFI surface as an API you'll maintain forever.
About the author
Jonas Kruckenberg
Senior Engineering Consultant @Mainmatter
Jonas Kruckenberg is a systems engineer and technologist focused on next-generation computing infrastructure. He is the lead author of k23, an experimental high-reliability operating system. As a TC39 Invited Expert, he helps shape the future of web by bringing non-browser perspectives to JavaScript language standardization.<br>Mainmatter is an engineering consultancy specializing in Rust. We offer engineering services, training, and mentorship to companies that aim to migrate from C to Rust or introduce Rust into an existing codebase.
Facing Rust challenges and need a helping hand?<br>We can help with complex C-to-Rust migrations, reliability issues, training, and skill gaps.<br>Contact us
Community<br>EuroRust 2026<br>EuroRust is a two-day conference for all things Rust, plus two more days of side activities, workshops, and hackathons. Join us in October in Barcelona.<br>Get your ticket
Opens external website in a new window.