A lightweight, experimental Rust REPL (Read-Eval-Print Loop) that allows you to interactively run Rust code snippets.
- Evaluate Expressions: Instantly see the results of Rust expressions.
- Define Items: Define functions, structs, enums, and macros that persist for the rest of the session.
- Multiline Input: Keep typing when a snippet is unfinished. The prompt switches from
->to..until delimiters, strings, comments, and outer attributes are closed. - Maintain State:
letbindings, assignments, and method calls (likevec.push()) are preserved, allowing you to build state incrementally. - On-the-fly Compilation: Compiles your snippets into dynamic libraries and loads them into the running process.
temp-rs uses a unique "replay" architecture to simulate a persistent environment:
- Parsing: Input is read until the snippet is complete, then classified as an Item (e.g.,
fn), a Statement (e.g.,let x = 1;), or an Expression (e.g.,x + 1). A trailing;after a braced item such asstruct A { n: u8 };is dropped so the item can be replayed at crate level. - Source Generation: For every new input, the engine generates a temporary Rust source file. This file includes:
- All previously defined Items.
- All previous Statements that modify state (like
letbindings ormutupdates). - The new input wrapped in a special
extern "C"function named__repl_eval.
- Compilation: The engine calls
rustcto compile the generated source into a dynamic library (cdylib). - Loading: The resulting library is loaded using
dlopen(or equivalent). - Execution: The engine looks up the
__repl_evalsymbol and executes it, printing the result if the input was an expression.
Start the REPL with:
just runThat runs cargo run -p temp-rs-cli. The same command works without just:
cargo run -p temp-rs-cliInstall the temp-rs binary onto your PATH:
just install
temp-rsjust install runs cargo install --path crates/temp-rs-cli.
Check formatting, lints, and tests:
just checkjust check runs cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, and cargo nextest run --workspace. cargo-nextest needs to be installed for that last step.
Once the REPL is running, you can type Rust code at the -> prompt:
Expressions:
-> 2 + 2
4Variables and State:
-> let mut x = 10
-> x += 5
-> x
15Functions and Items:
-> fn double(n: i32) -> i32 { n * 2 }
-> double(x)
30Complex Types:
-> let mut v = vec![1, 2]
-> v.push(3)
-> v
[1, 2, 3]Multiline items:
Enter on an unfinished snippet switches the prompt to .. and does not compile yet. Ctrl-C discards the snippet and returns to ->. A semicolon after a braced struct is accepted and the struct is kept for later inputs. Defining an item prints nothing.
-> #[derive(Debug)]
.. struct A {
.. n: u8,
.. };
-> let a = A {
.. n: 10,
.. };
-> a
A { n: 10 }crates/temp-rs-cli: The command-line interface and REPL loop.crates/temp-rs-core: The high-level engine that manages history and evaluation.crates/temp-rs-compiler: Handles Rust code parsing, source generation, andrustcinvocation.crates/temp-rs-dylib-loader: A minimal wrapper for loading and searching dynamic libraries.
- Because every evaluation recompiles the entire history of items and bindings, performance may degrade as the session history grows very large.
- External crate dependencies are not yet supported in the REPL environment.