Introduction
Rust has emerged as a powerful systems programming language by promising the performance of C++ alongside the memory safety of high-level languages. This is achieved through a unique system of ownership, moves, and borrows. However, these mechanisms are notoriously difficult for developers to internalize, and until recently, lacked a formal, executable model for the source language. Researchers have addressed this by developing KRust, the first formal, executable operational semantics for a realistic subset of Rust.
Formalizing the Rust Subset
Built using the $\mathbb{K}$ framework, KRust provides a machine-readable model that automatically generates a working interpreter and formal verification tools. The core of the project involves defining the language's syntax using EBNF and its state transitions through a configuration of 13 specific "cells." The syntax covers a broad range of Rust features, including primitive types (i8, u32, f64, etc.), structured data like structs and arrays, and complex control flow statements such as loop, while, and if-else.
In the $\mathbb{K}$ framework, these cells track the program's evolving state. To handle Rust’s unique features, specialized cells were designed: mutType tracks whether a variable is mutable or immutable, borrow records if a variable is currently lent out, and moved tracks whether a value has been transferred to a new owner. Other cells like refType distinguish between different types of references, while nextLoc manages memory allocation. This granular tracking allows KRust to prevent "use-after-move" errors at the semantic level.
Executable Semantics and Ownership
Unlike traditional "paper-only" semantics, KRust's executable nature allows it to serve as a reference implementation. The semantics explicitly model Rust's "substructural" variable context, where a variable can be rendered unusable (moved) even while still in scope. This is a significant departure from traditional languages like C or Java.
As illustrated in the semantic rules, KRust manages state transitions by updating the environment and store cells. For example, the rules for mutable and immutable references ensure that the borrow checker's logic is encoded directly into the operational semantics. By defining the language this way, the $\mathbb{K}$ framework automatically yields a suite of tools, including a debugger and a symbolic execution engine.
Validation and Tooling
To ensure high fidelity, the authors validated KRust against 157 tests from the official Rust test suite and 25 custom tests designed to cover edge cases. KRust successfully passed all 182 tests, with its interpreter matching the output of the actual Rust compiler (rustc) exactly.
The researchers also demonstrated the power of automated verification. Using the tools generated by the $\mathbb{K}$ framework, they developed a "sound-by-construction" program verifier and used it to formally prove the time complexity of a Euclidean GCD algorithm written in Rust. Additionally, KRust enables interactive debugging, allowing developers to step through a program rule-by-rule to observe exactly when a variable is moved or borrowed at the semantic level.
Conclusion and Future Work
KRust represents a major step forward in the formal verification of Rust. While it currently omits features like concurrency, the unsafe keyword, and traits, it serves as a foundational "cornerstone" for a complete, formally verified Rust development environment. The authors view KRust as the first step in a long-term project to provide developers with a robust, mathematically sound framework for building safe systems software. Future work aims to expand the supported syntax and integrate more complex features like lifetime annotations and closures.
Summary updated