C2Rust helps you migrate C99-compliant code to Rust.
The transpiler, c2rust transpile,
produces unsafe Rust code that closely mirrors the input C code.
The primary goal of the transpiler is to preserve functionality;
test suites should continue to pass after translation.
The output of c2rust transpile is unsafe and unidiomatic; it is merely the first step in a longer migration process.
Generating safe and idiomatic Rust code from C ultimately requires additional work.
This work can be done by a human, a large language model, a deterministic tool, or some combination thereof.
flowchart LR
A[C sources] --> B
subgraph "C2Rust toolchain"
B["`c2rust transpile`"] --> C["`c2rust refactor`"]
C --> D["`c2rust postprocess`"]
end
D --> E[Human and/or agentic workflow]
E --> F[Safe, idiomatic Rust]
click B "https://github.com/immunant/c2rust/tree/master/c2rust-transpile" "c2rust transpile"
click C "https://github.com/immunant/c2rust/tree/master/c2rust-refactor" "c2rust refactor"
click D "https://github.com/immunant/c2rust/tree/master/c2rust-postprocess" "c2rust postprocess"
For instance, we provide a deterministic refactoring tool to automate cleanup across the files produced by c2rust transpile.
We also provide an LLM-powered postprocessing tool for additional types of cleanup that are hard to do deterministically.
Even though the postprocessor validates the output of LLMs, it can introduce errors; we recommend using it in combination with a robust test suite.
You can also cross-check the translated code against the original (tutorial).
You can try c2rust transpile directly in the Compiler Explorer.
This uses the current master branch, updated every night.
To learn more about using and developing C2Rust, check out the manual. The manual is still a work-in-progress, so if you can't find something please let us know. c2rust.com/manual/ also has not been updated since ~2019, so refer to the in-tree