A REPL for Rust!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Dylan Baker fa6f4c890c Fix ~ not getting expanded 11 月之前
src Fix ~ not getting expanded 11 月之前
.gitignore Initial commit 1 年之前
Cargo.lock Change package name 1 年之前
Cargo.toml Update name 1 年之前
LICENSE Add license 1 年之前
README.md Gitea's Markdown renderer doesn't like `- ####` 1 年之前

README.md

Rust REPL

Rust REPL is a command-line tool to run some Rust code quickly in a REPL environment, similar to what interepretated languages like Lisp, Python, and Ruby provide.

This REPL should function as you expect for the most part. There are a couple of special features to note though. First, unlike in Rust source files, you can omit semi-colons and they will be inserted for you automatically before being given to rustc. Second, the REPL provides two commands, p! and d!, which are syntactic sugar for println("{}", <var>) and println("{:?}", <var>), respectively (p for ‘print’ and d for ‘debug’). For example:

rust> p! "hello world"
hello world
rust> d! "hello world".find('h')
Some(0)

How Does It Work?

The code you enter into the REPL gets wrapped in fn main() { ... }, written to a file, and given to rustc. The REPL then shells out to the resulting binary and captures any output. All lines of code in a given REPL session are written to the same file, which allows you to reference variables from previous lines. For example:

rust> let x = 5
rust> let y = x + 1
rust> p! x
5
rust> p! y
6

The only exception to this is that calls to print! and println! (which includes usage of the p! and d! commands) are discarded after running so as to not clutter up the rest of the session.

Caveats

Explicit Printing

Normally, entering a value into a REPL prints that value right back at you without you having to specify that you want to print it. For example:

python> 5 + 5
10

Rust REPL currently requires you to explicitly print anything you want to be printed. Since it works by shelling out to rustc, it doesn’t have direct access to the return values of your expressions, only to whatever gets written to stdout. I’m exploring the idea of automatically insert calls to println! for you, but since not all expressions can be printed, I need to do some experimentation.

Isolated Execution Context

There is currently no way to run a REPL in the context of a Rust project, like Lisp developers will be used to doing. Again, this might happen in the future but probably not any time soon.

Installation

$ git clone https://git.simulacrum.party/simulacrumparty/rust-repl
$ cd rust-repl
$ cargo install --path .

Usage

USAGE:
    rust-repl [OPTIONS]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -d, --directory <DIRECTORY>    The directory where Rust REPL should store files.
                                   Default: ~/.rust-repl

License

This software is provided under the terms of the MIT License