Cpp2Rust: Automatic Translation of C++ to Safe Rust
Summary
Cpp2Rust is an open-source tool that automatically translates C++ code to safe Rust using clang's AST and a runtime library, enabling safe memory-safe conversion with support for both safe and unsafe output modes.
View Cached Full Text
Cached at: 07/10/26, 06:05 AM
Cpp2Rust/cpp2rust
Source: https://github.com/Cpp2Rust/cpp2rust
Cpp2Rust
Cpp2Rust translates C++ to fully safe Rust automatically. It is a syntax-driven translator based on clang’s AST.
Cpp2Rust’s algorithm is described in the paper Cpp2Rust: Automatic Translation of C++ to Safe Rust published at PLDI 2026.
Overview
Cpp2Rust first parses the input C++ file(s) with clang and produces an AST.
It then traverses the AST and emits Rust code as strings, inserting
calls to the libcc2rs runtime library where needed (e.g., for raw pointer
semantics).
Finally, the Rust code is pretty-printed using rustfmt to a single .rs file.
By default the reference counting model is used, which produces fully safe
Rust.
A generator of unsafe Rust is also available through the --model=unsafe
command line argument for debugging and performance comparisons.
Runtime library (libcc2rs)
The generated code relies on a runtime library designed to simplify the
translation process.
C pointers are converted into the Ptr<T> type provided by libcc2rs.
Ptr<T> models C pointer semantics, including null, arithmetic, and aliasing,
while satisfying Rust’s borrow checker through checked run-time operations.
Requirements
On Ubuntu, install the required dependencies with:
sudo apt install libclang-22-dev clang++-22 ninja-build cmake
curl -LsSf https://astral.sh/ruff/install.sh | sh
Build
mkdir build
cd build
cmake -GNinja ..
ninja
ninja check
Run
Translate a single file
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs
By default, the reference counting model is used (fully safe output). To generate unsafe Rust instead:
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs --model=unsafe
Minimal example. Given hello.cpp:
#include <cstdio>
int main() {
printf("hello world\n");
return 0;
}
Running ./build/cpp2rust/cpp2rust --file=hello.cpp -o=hello.rs produces:
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
println!("hello world");
return 0;
}
Compile and run with:
rustc hello.rs -L ../libcc2rs/target/debug
./hello
Translate a whole program
First generate a
compile_commands.json
for your project. With CMake this is one extra flag:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
Then run:
./build/cpp2rust/cpp2rust --dir=<dir> -o <output>.rs
<dir> must be the directory that contains compile_commands.json.
Test Suite
# Run all tests
ninja check
# Run only the unit tests
ninja check-unit
# Run libcc2rs unit tests
ninja check-libcc2rs
# Run libcc2rs-macros unit tests
ninja check-libcc2rs-macros
# Regenerate expected output for unit tests after intentional changes
REPLACE_EXPECTED=1 ninja check-unit
Similar Articles
A line-by-line translation of the OCaml runtime from C to Rust
The project details a line-by-line translation of the OCaml runtime from C to Rust, aiming to improve safety and performance.
crustc: Entirety of rustc, translated to C
A Rust-to-C compiler toolchain called cilly has successfully translated the entire rustc compiler into 46 million lines of C, resulting in a functional Rust compiler that can be built with GCC. The project aims to enable Rust on old/obscure hardware by generating portable C code.
I made rust's cargo copy but for CPP
CRow is a new open-source build system and dependency manager for C/C++ that mimics the simplicity of Rust's Cargo.
How (and why) we rewrote our production C++ frontend infrastructure in Rust
NearlyFreeSpeech.NET rewrote their production C++ frontend infrastructure (nfsncore) in Rust, a critical system that handles routing, caching, and access control for all incoming requests. The migration was motivated by Rust's safety guarantees, performance, ecosystem strength, and the aging C++ codebase's limitations.
Port React Compiler to Rust
An initiative to port the React Compiler to Rust, aiming to improve performance and integration with the Rust ecosystem.