Crown is an experimental C-to-Rust converter for common, small console programs. It performs source-level rewrites; it is not a complete C/C++ compiler or a security tool. Generated code must be reviewed, compiled, behavior-tested, and manually audited.
The converter currently handles these common patterns when written in a straightforward, one-statement-per-line style:
mainand simple functions with scalar parameters/return types;- scalar local declarations, including comma-separated variables, and fixed-size one- or multi-dimensional arrays with brace initializers;
- simple
if,else if,while,do/while, and monotonicforloops using<,<=,>, or>=with++,--, or a literal step; - basic
switchstatements with singlecaselabels andbreak, plus simple ternary expressions; - standalone
++/--statements; printfwith common%d,%i,%u,%f,%lf,%c,%s,%x,%X,%oand%%formats, including multiple arguments;- token-based
%d,%i,%u,%f,%lf,%c, and%sscanfinput inmain; return 0;frommainand simple#includeremoval.
For example, this program:
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}converts to Rust with mutable i32 locals, line-based stdin token parsing, and Rust formatting macros. The generated version is saved as tests/output.rs, with the C fixture in tests/input.c.
C and Rust have different type systems, memory models, and runtime behavior. There is no universal syntax-to-syntax conversion that can make arbitrary C valid and safe Rust. Crown currently does not translate pointer ownership, structs, unions, enums, function pointers, macros, dynamic allocation, file I/O, complex declarators, or most library APIs. C format strings and input behavior are only approximated for the listed conversions. Unsupported lines may remain C and make the output fail to compile; even compilable output may change behavior. The preflight parser checks only delimiter/string/comment balance, not C syntax or types.
Crown emits a general review warning. Do not treat generated output as safe or production-ready without review.
Requires stable Rust and Cargo; no Clang/LLVM installation is needed.
cargo test
cargo build --release
cargo run -- -i input.c # write Rust to stdout
cargo run -- -i input.c -o output.rs # write Rust to a fileUse -o - to explicitly select stdout. Errors are reported on stderr with a non-zero exit status. Run rustfmt, compile the generated Rust, compare behavior against the C program, and manually audit memory safety before using it.
Contributions are welcome. Add regression tests for each new conversion and document behavior changes and unsupported cases. Never imply that translating a pointer or allocation expression automatically makes it safe.
GPL-3.0. See LICENSE.