Skip to content
HN On Hacker News ↗

GitHub - Cpp2Rust/cpp2rust: Cpp2Rust: Automatic Translation of C++ to Safe Rust

▲ 57 points • 20 comments • by signa11 • 4mo ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this document is fully human-written

3 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 2 of 2
SEGMENTS · AI 0 of 2
WORD COUNT 348
PEAK AI % 3% · §1
Analyzed
Jul 10
backend: pangram/v3.3
Segments scanned
2 windows
avg 174 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 348 words · 2 segments analyzed

Human AI-generated
§1 Human · 3%

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.

§2 Human · 2%

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