|
3 months ago | |
---|---|---|
src | 3 months ago | |
.gitignore | 6 months ago | |
Cargo.lock | 6 months ago | |
Cargo.toml | 6 months ago | |
LICENSE | 6 months ago | |
README.md | 6 months ago |
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)
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.
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.
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.
$ git clone https://git.simulacrum.party/simulacrumparty/rust-repl
$ cd rust-repl
$ cargo install --path .
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
This software is provided under the terms of the MIT License