Skip to main content

The Rust Compiler Was Originally Written in OCaml

One of the quieter secrets of Rust's origin story is that the first compiler striving to enforce memory safety was itself written in a language famous for strong static typing: OCaml. When Graydon Hoare began sketching Rust in 2006, he chose OCaml for the initial compiler not as a compromise, but because it was well suited to building the kind of sophisticated type system Rust demanded. It let the team prototype advanced type checking and ownership rules without first building a whole toolchain for a language that did not even exist yet.

In that early phase, the project was a research prototype. Graydon worked at Mozilla, and Rust grew from his side experiments while the company considered what a safer systems language might look like. Using OCaml meant the front end, the type checker, the borrow checker, all of it could be developed quickly, because OCaml is excellent at exactly this work. It has algebraic data types, pattern matching, and a powerful module system, features that map naturally onto compiler construction.

The logical tension of that arrangement is worth sitting with: the language was solving one of the hardest problems in systems programming, yet anyone who wanted to contribute to the compiler had to know OCaml, not Rust. The community was small and the ecosystem for OCaml tooling was narrow compared to what Rust now enjoys. As the language matured and adoption spread, this dependency became a growing bottleneck for getting new developers into the compiler's inner workings.

So the team did something ambitious that has since become a rite of passage for young languages: they made rustc self hosting. The goal was to have Rust compile itself, so that the compiler is built in its own language by its own toolchain. This bootstrapping effort quietly ran for years. Eventually the OCaml based compiler became the seed, and a Rust based compiler was written to take over, releasing rustc from its OCaml dependency altogether.

The self hosting milestone matters far beyond sentiment. When the compiler is written in Rust, the project dogfoods its own ideas, tens of thousands of lines of real world systems code battle testing the borrow checker and the standard library on a daily basis. LLVM, which sits underneath rustc as the code generation backend, is still largely in C and C++, so the full stack is not entirely Rust, but the language's own development environment is now firmly in its own hands.

For a senior developer, this history is a reminder that choosing the right tool for building tools is a distinct skill. Rust's early adoption of OCaml was not a detour or a failure; it was the fastest path to a working type checker. Then, once the language had enough users and maturity, the community made the deliberate investment to cut the cord and become its own host. That pattern, use the best tool available now, then reinvest to remove the dependency, repeats constantly in real engineering.

The practical lesson stands even for people who never write a compiler. Bootstrapping is how you prove a language is real and durable. Rust compiling itself in Rust is a kind of stress test that very few languages pass. Every time a newer Rust release is built and shipped, the toolchain is exercising Rust against Real Rust, and that daily feedback loop is part of why the language keeps getting faster and more reliable as a systems implementation.

We use cookies for ads and analytics.