explaingit

immunant/c2rust

4,697RustAudience · developerComplexity · 4/5Setup · hard

TLDR

C2Rust automatically converts existing C code into Rust code so you can start migrating legacy C programs to memory-safe Rust without rewriting everything from scratch.

Mindmap

mindmap
  root((C2Rust))
    What it does
      C to Rust transpile
      Behavior preservation
      Cross-checking
    Workflow
      Auto convert C code
      Verify with test suite
      Refactor to safe Rust
    Tech Stack
      Rust
      C
      LLVM
    Audience
      Systems developers
      Rust adopters
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automatically transpile an existing C codebase into Rust to begin a migration to memory-safe code.

USE CASE 2

Run your original C test suite against the transpiled Rust output to verify the behavior is identical.

USE CASE 3

Gradually refactor unsafe Rust output from the transpiler into safe idiomatic Rust using the included refactoring tool.

Tech stack

RustCLLVM

Getting it running

Difficulty · hard Time to first run · 1day+

Requires a full C compiler toolchain plus LLVM system libraries in addition to the Rust toolchain.

In plain English

C2Rust is a tool for developers who want to take existing C code and convert it into Rust code automatically. Rust is a programming language designed to prevent common memory-related bugs and security vulnerabilities that are frequent in C programs. The conversion process is called transpilation, and C2Rust handles the mechanical parts of that translation so you do not have to rewrite everything from scratch. The tool reads your C source files and produces Rust code that does the same things your C code did. The resulting Rust code is not written in a clean or idiomatic Rust style, it uses an internal mode called unsafe Rust that closely mirrors the original C structure. The main goal is that your test suite should still pass after the conversion, meaning the behavior is preserved even if the code style is rough. Because the output is unsafe Rust rather than fully safe Rust, there is still manual work to do after the automatic step. The project includes a separate refactoring tool meant to help with that process, letting you gradually clean up the translated code and shift more of it into Rust's safe subset. The README notes this refactoring work is still in early stages. There is also a cross-checking feature that lets you run both the original C program and the translated Rust program at the same time against the same inputs, comparing their outputs to verify the translation did not introduce any differences. This is useful for catching subtle behavioral mismatches before you retire the C version. Installing C2Rust requires a C compiler toolchain and the LLVM system libraries in addition to the Rust toolchain itself. Setup instructions are provided for Ubuntu, Arch Linux, macOS, and NixOS. The transpiler can be installed from the standard Rust package registry or directly from the project's source code repository.

Copy-paste prompts

Prompt 1
Using C2Rust, transpile my C library at ./src/ into Rust, show me the exact commands and what to expect in the output.
Prompt 2
After running C2Rust on my C project, how do I use the cross-checking feature to run both the C and Rust versions in parallel and compare outputs?
Prompt 3
Which parts of my C2Rust transpiler output are safest to refactor first, and how do I use the refactoring tool to remove unsafe blocks?
Prompt 4
How do I install C2Rust on Ubuntu, including all the LLVM and Clang system library dependencies it requires?
Open on GitHub → Explain another repo

← immunant on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.