Virtual Rust 🦀
A virtual machine that interprets and runs Rust source code directly — no compilation needed.
Virtual Rust is a Rust interpreter written in Rust. It reads .rs source files (or inline expressions) and executes them immediately via a Lexer → Parser → AST → Tree-walking Interpreter pipeline.
Quick Start
# Build
# Run a Rust source file directly
# Run a cargo project with multiple binaries
# Evaluate an expression
# Start interactive REPL
Features
Language Support
| Feature | Status |
|---|---|
Variables (let, let mut) |
✅ |
| Type annotations & inference | ✅ |
Functions (fn) with params & return types |
✅ |
Closures (|x| x * 2) |
✅ |
if / else if / else (statement & expression) |
✅ |
while loops |
✅ |
loop with break value |
✅ |
for..in with ranges |
✅ |
match with patterns, ranges, wildcards |
✅ |
| Arithmetic / comparison / logical / bitwise ops | ✅ |
Compound assignment (+=, -=, *=, /=, %=) |
✅ |
Type casting (as) |
✅ |
Arrays, Vec, tuples |
✅ |
| Structs with field access | ✅ |
| String, char, integer, float, bool types | ✅ |
| Nested functions and recursion | ✅ |
Higher-order functions (map, filter, fold) |
✅ |
Iterator chains (.iter().map().filter().collect()) |
✅ |
Comments (// and /* */) |
✅ |
Attributes (#[...]) skip |
✅ |
Built-in Macros
println!,print!,eprintln!,eprint!— with format string support ({},{:?},{:.2})format!— string formattingvec!— vector creationassert!,assert_eq!,assert_ne!— assertionsdbg!— debug printingpanic!,todo!,unimplemented!,unreachable!include_str!— file reading
Methods
String: len, is_empty, contains, starts_with, ends_with, trim, to_uppercase, to_lowercase, replace, split, chars, bytes, push_str, repeat, lines, parse
Array/Vec: len, is_empty, push, pop, first, last, contains, reverse, iter, map, filter, fold, for_each, enumerate, zip, sum, product, min, max, join, any, all, find, position, skip, take, count, flat_map, collect
Integer: abs, pow, min, max, clamp, to_string
Float: abs, sqrt, floor, ceil, round, sin, cos, tan, ln, log2, log10, powi, powf, is_nan, is_infinite, is_finite
Option: unwrap, unwrap_or, is_some, is_none, map
Char: is_alphabetic, is_numeric, is_alphanumeric, is_whitespace, is_uppercase, is_lowercase
Usage
# Run a file
# Inline evaluation
# Interactive REPL
# Help
REPL Commands
| Command | Description |
|---|---|
:quit, :q |
Exit the REPL |
:help, :h |
Show help |
:clear, :c |
Clear environment |
:version, :v |
Show version |
Running Cargo Projects
Virtual Rust can run entire Cargo project directories. Extra arguments after -- are passed directly to cargo run, so you can use any cargo flags like --bin, --release, etc.
# Run a Cargo project directory
# Run a specific binary in a multi-bin project
# Pass both cargo flags and program arguments
# Build in release mode
The examples/multi_bin project demonstrates this — it defines three binaries (server, client, health) and requires --bin <name> to select which one to run.
Running Loose .rs Directories
Virtual Rust can also run a directory of plain .rs files that have no Cargo.toml. It automatically:
- Finds the entry point (the file containing
fn main()) - Generates a temporary Cargo project
- Copies the entry file as
src/main.rsand all other.rsfiles as modules - Compiles and runs the project
# Given a folder with main.rs, math.rs, greeting.rs (no Cargo.toml)
The entry file can use mod to reference sibling files:
// main.rs
The examples/loose_modules directory demonstrates this pattern.
Examples
// examples/fibonacci.rs
) =
) =
) =
) =
) =
) =
) =
) =
) =
) =
Architecture
Source Code (.rs)
│
▼
┌────────┐
│ Lexer │ Tokenizes source into tokens
└───┬────┘
│ Vec<Token>
▼
┌────────┐
│ Parser │ Builds Abstract Syntax Tree
└───┬────┘
│ Vec<Expr> (AST)
▼
┌──────────────┐
│ Interpreter │ Tree-walking execution
└──────────────┘
│
▼
Output
License
MIT