CRUST is a command-line transpiler that converts C/C++ syntax into Rust code. It aims to reduce migration effort while preserving semantics with safe, auditable output.
- Converts declarations, assignments, and basic expressions
- Supports control flow:
if/if-else,while,for,switch - Handles basic struct/union/class definitions
- Preserves comments and outputs warning markers for unsupported constructs
- Strict (
-s) vs loose mode (default) semantics for variable mutability
Works best for small/medium C++ code slices. Complex constructs (templates, macros, function pointers) are partially supported and may require manual adjustments.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustc --versiongit clone https://github.com/nishanthspshetty/crust.git
cd crustcargo buildcargo run -- examples/prog.cppInteractive prompts:
Enter the C/C++ file to be converted to Rust : examples/prog.cppEnter the translation mode [(S/s)trict/(L/l)oose] : lDo you want to create a cargo project :[Y/N] n
Output is written to examples/prog.rs by default (or <project>/src/main.rs if project creation is selected).
cargo run -- -s -p myproj examples/main.cpp-s,--strict: strict mode (immutable bindings)-p,--project-name: create a new Cargo project and write toproject/src/main.rs-h,--help: display usage information
Run the full test suite:
cargo testKey test locations:
src/library/parser/parser_test.rssrc/library/lexer/tokenizer/*
- full expression output validation for declarations and expressions
- parser bounds-safe checks to avoid index panics
- strict/loose mode behavior tests
src/main.rs: CLI + orchestration + file I/Osrc/library/lexer: tokenization pipelinesrc/library/parser: recursive descent translation engine
- Tokenize input into
Vec<Token> init_parserprocesses withParserstateparse_programdispatches to specialized handlers (control flow, types, assignments)- Build Rust token stream and output text
- avoid
Veccloning; use&[Token]slices - avoid
expectin parser production paths; useResultwith structured errors next - enforce
cargo fmt,cargo clippy
cargo test
cargo fmt --all
cargo clippy -- -D warnings- Types:
int,float,char,const,unsigned, pointers - Control structures:
if,if-else,while,for,switch typedef,struct,union,class,enum- Comments preserved (as
//or/* */blocks)
- templates, generic C++ constructs
- function pointers and functors
- macros with preprocessor logic
- comprehensive standard library conversion (e.g., iostream / std::vector)
- fixed Cargo metadata (
excludein[package]) - enabled robust parser bounds safety
- added full-expression tests for parser outputs
- expanded README with user + developer details, badges, screenshot placeholder
- Fork repository
- branch:
feature/<name> - Add/update tests in
src/library/parser/parser_test.rs cargo testandcargo fmt- PR with summary of behavior regressions/improvements
MIT