Natural language helps improve Rust code quality from C translations
Beyond Source Code: Evaluating Natural Language Specifications for LLM-Based C-to-Rust Translation
Software Engineering
Summary
Translating C programs into Rust code usually relies on the original C code itself. The authors found that using natural language descriptions of what the program should do can help large language models create Rust code that is cleaner, safer, and more in line with Rust coding practices. However, relying only on descriptions can lower correctness, so combining code and specifications and using testing steps leads to the best balance of correct and high-quality Rust code. This study compares ways to translate from C to Rust, showing trade-offs between accuracy, code quality, and cost.
What this means in practice
- •For software engineers: Generate safer and more maintainable Rust code from legacy C programs by combining source code and natural language descriptions with iterative testing.
- •For devops teams: Improve automated code translation workflows by integrating specification-based validation to balance correctness and code quality for system migrations.
Authors
Biruk Tadesse, Lawrence Arkoh, Marcelo D'Amorim, Wesley K. G. Assunção
Abstract
Large language models (LLMs) have shown promising results for C-to-Rust translation, yet existing approaches primarily rely on source code as input, leaving natural-language specifications of program behavior underexplored as an alternative or complementary representation. We hypothesize that direct code translation encourages LLMs to preserve C-specific implementation choices, producing Rust that may be functionally correct but unnecessarily verbose, reliant on unsafe constructs, and less idiomatic. We investigate this hypothesis by comparing three translation settings: code-only, specification-only, and code-and-specification. Using 300 C programs from the IBM CodeNet dataset, we evaluate GPT-5.2 and GPT-4o-mini on functional correctness, maintainability, complexity, idiomaticity, unsafe construct usage, and cost. Code-only translation achieves the highest test pass rate (98.6%) but often mirrors C's implementation patterns. In contrast, specification-only translation produces the most idiomatic and compact Rust, reducing unsafe blocks by approximately 6x and Clippy style warnings by 2.4x. However, using specifications without validation yields test pass rates as low as 86% with our best performing model. Adding an iterative test-based validation and refinement step for specifications raises this to a 95.3% test pass rate. Overall, specification-based translation consistently improves code quality relative to code-only translation, while code-and-specification offers the best balance between correctness (98.3% test pass rate) and maintainability. Our study systematically compares these translation settings, highlighting the trade-offs among specification quality, translation correctness, code quality, and computational cost.