Skip to content

Latest commit

 

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 rust_eval

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.


Features

  • Interactive Multi-Line Editing: Write and edit multi-line Rust code with seamless cursor navigation. Pressing Left () at the start of a line moves up to the previous line, and pressing Right () at the end of a line moves down to the next line. You can also use Up () and Down () to move vertically between lines and edit any line in place.
  • Input Streaming & EOF Evaluation: Type code naturally line-by-line with Enter. Stream EOF (Ctrl+D on Unix, Ctrl+Z on Windows) to submit the code for evaluation.
  • Regex Validation & Implicit main Wrapping: Uses pre-compiled regex matching to verify the presence of a valid fn main entry point. If no main function or additional function definitions (fn ) are present, it automatically wraps the snippet inside an implicit main function.
  • Cross-Platform Support: Automatically handles execution targets and temp paths for Unix (Linux/macOS) and Windows (.exe).
  • Isolated Build Environment: Automatically manages a temporary directory in the system temp folder (std::env::temp_dir() / "rust-eval") for source generation and compilation, ensuring thorough cleanup even on error.
  • Detailed Execution Output: Displays structured reports for STDOUT, STDERR, and the process Exit Code.

How It Works

  1. Input Reading: Displays a >>> prompt and accepts multi-line code until EOF (Ctrl+D on Unix/macOS, Ctrl+Z on Windows) is signaled. You can navigate left/right/up/down to edit any line above before submitting.
  2. Validation & Wrapping: Evaluates input against a LazyLock<Regex> pattern. If a valid fn main(...) signature exists, it uses the input as-is. If no main exists and no other functions are defined, it automatically wraps the input inside fn main() { ... }. If other functions exist without a main, it returns an error.
  3. Source Preparation: Prepares the temporary directory and outputs tmp.rs, wrapped with auto-generated headers and footers.
  4. Compilation & Execution: Invokes rustc to compile tmp.rs into the temporary directory. On successful compilation, executes the generated binary (tmp or tmp.exe), passing stdin, stdout, and stderr directly through to the terminal process.
  5. Cleanup: Cleans up the temporary workspace upon successful execution or runtime failures.

Getting Started

Prerequisites

  • Rust toolchain installed with rustc available in your system PATH.

Running locally

  1. Clone the repository:

    git clone https://github.com/Jacob-Dayan/rust-eval.git
    cd rust_eval
  2. Run using cargo:

    cargo run

Example Usage

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:

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);

Project Structure

.
├── 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

License

This project is licensed under the MIT License.

About

A lightweight, wrapper-based Rust REPL-like eval that dynamically compiles and executes code snippets using rustc

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages