rust_eval is a lightweight Rust CLI tool that reads Rust source code interactively from standard input (stdin), validates it, dynamically compiles it using rustc, and executes it on the fly in an Eval-style workflow.
- Interactive Input: Stream Rust scripts directly from
stdinuntil anEOFsignal is received. - Regex Validation & Implicit
mainWrapping: Uses pre-compiled regex matching to verify the presence of a validfn mainentry point. If nomainfunction or additional function definitions (fn) are present, it automatically wraps the snippet inside an implicitmainfunction. - Cross-Platform Support: Automatically handles execution targets and temp paths for Unix (
Linux/macOS) andWindows(.exe). - Isolated Build Environment: Automatically manages a temporary directory (
tmp/) for source generation and compilation, ensuring thorough cleanup even on error. - Detailed Execution Output: Displays structured reports for
STDOUT,STDERR, and the processExit Code.
- Input Reading: Displays a
>>>prompt and streams user code untilEOF(Ctrl+Don Unix/macOS,Ctrl+Zon Windows) is signaled. - Validation & Wrapping: Evaluates input against a
LazyLock<Regex>pattern. If a validfn main(...)signature exists, it uses the input as-is. If nomainexists and no other functions are defined, it automatically wraps the input insidefn main() { ... }. If other functions exist without amain, it returns an error. - Source Preparation: Creates the
tmp/workspace and outputstmp/tmp.rs, wrapped with auto-generated headers and footers. - Compilation & Execution: Invokes
rustc tmp/tmp.rs --out-dir tmp. On successful compilation, executes the generated binary (tmp/tmportmp/tmp.exe), passingstdin,stdout, andstderrdirectly through to the terminal process. - Cleanup: Cleans up the temporary workspace upon successful execution or runtime failures.
- Rust toolchain installed with
rustcavailable in your systemPATH.
-
Clone the repository:
git clone https://github.com/Jacob-Dayan/rust-eval.git cd rust_eval -
Run using
cargo:cargo run
Upon starting the binary:
To enter, stream EOF (ctrl+D on Unix, ctrl+Z on Windows)
>>> fn main() {
let numbers = vec![1, 2, 3, 4, 5];
let sum: i32 = numbers.iter().sum();
println!("The sum is: {}", sum);
}
Press Ctrl+D (or Ctrl+Z on Windows) to trigger evaluation:
The sum is: 15
And if you just wanna run a simple code snippet, you don't even have to mess with a main function.
The code below works exactly the same as the one above:
To enter, stream EOF (ctrl+D on Unix, ctrl+Z on Windows)
>>> let numbers = vec![1, 2, 3, 4, 5];
let sum: i32 = numbers.iter().sum();
println!("The sum is: {}", sum);
.
├── src/
│ ├── lib.rs # Exported macros and modules
│ ├── consts.rs # Path constants, Regex matchers, Header & Footer wrappers
│ └── main.rs # CLI entry point, input handling, process control, and cleanup
└── Cargo.toml
This project is licensed under the MIT License.