[Awesome Rust] Use evcxr_repl to run Interactive Rust

evcxr_repl

evcxr_repl is A Rust REPL (Read-Eval-Print loop) built to provide evaluation context for Interactive programming.

Interactive programming, in which expressions are entered and evaluated in real-time, can be a powerful tool for exploring a language and problem solving. This capability is most often associated with dynamically evaluated languages such as JavaScript and Python. Compiled languages such as Java and C++ can also be used interactively, but tooling tends to lag by many years. Rust is a newer compiled language for which interactive programming has recently emerged. This article discusses interactive programming with Rust courtesy of the google/evcxr - https://github.com/google/evcxr crate.

Prerequites

Cargo is the Rust package manager. Cargo downloads your Rust package’s dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io - https://crates.io/, the Rust community’s package registry.

  • Install Rust and Cargo

The easiest way to get Cargo is to install the current stable release of Rust by using rustup. Installing Rust using rustup will also install cargo.

On Linux and macOS systems, this is done as follows:

1
$ curl https://sh.rustup.rs -sSf | sh

It will download a script, and start the installation. If everything goes well, you’ll see this appear:

1
Rust is installed now. Great!

Installation

Installed with the following command:

1
$ cargo install evcxr_repl

Usages

And start the REPL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ evcxr
Welcome to evcxr. For help, type :help
:help
>> :help
:clear Clear all state, keeping compilation cache
:dep Add dependency. e.g. :dep regex = "1.0"
:efmt Set the formatter for errors returned by ?
:explain Print explanation of last error
:fmt Set output formatter (default: {:?})
:help Print command help
:internal_debug Toggle various internal debugging code
:last_compile_dir Print the directory in which we last compiled
:last_error_json Print the last compilation error as JSON (for debugging)
:linker Set/print linker. Supported: system, lld
:load_config Reloads startup configuration files
:offline Set offline mode when invoking cargo
:opt Set optimization level (0/1/2)
:preserve_vars_on_panic Try to keep vars on panic (0/1)
:quit Quit evaluation and exit
:sccache Set whether to use sccache (0/1).
:time_passes Toggle printing of rustc pass times (requires nightly)
:timing Toggle printing of how long evaluations take
:toolchain Set which toolchain to use (e.g. nightly)
:vars List bound variables and their types
:version Print Evcxr version

>> let x = 5;
>> println!("{}", x)
5
()
>> :quit

See Usages - Google Search - https://github.com/google/evcxr/blob/main/COMMON.md to learn more Usages.

References

[1] evcxr/README.md at main · google/evcxr - https://github.com/google/evcxr/blob/main/evcxr_repl/README.md

[2] Usages - Google Search - https://github.com/google/evcxr/blob/main/COMMON.md

[3] google/evcxr - https://github.com/google/evcxr

[4] Interactive Rust in a REPL and Jupyter Notebook with EVCXR | Depth-First - https://depth-first.com/articles/2020/09/21/interactive-rust-in-a-repl-and-jupyter-notebook-with-evcxr/

[5] evcxr - crates.io: Rust Package Registry - https://crates.io/crates/evcxr

[6] Introduction - The Cargo Book - https://doc.rust-lang.org/cargo/index.html

[7] Rust Programming Language - https://www.rust-lang.org/