The C to Rust migration book
Foreign Function Interfaces
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.
Types and Data Across the Boundary
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.
FFI-Safe Types
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.
FFI Validation
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.
Error Handling and API Design
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.
Production-Quality FFI
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.
Facing Rust challenges and need a helping hand?
We can help with complex C-to-Rust migrations, reliability issues, training, and skill gaps. Contact us.
Community
EuroRust 2026 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. Get your ticket.
Comments
No comments yet. Start the discussion.