13 releases
0.0.13 | Nov 22, 2022 |
---|---|
0.0.12 | Oct 18, 2020 |
0.0.11 | Jul 26, 2018 |
0.0.10 | Jan 27, 2018 |
0.0.6 | Dec 12, 2016 |
#371 in Development tools
34 downloads per month
12KB
161 lines
evalrs
Rust code snippet evaluator.
This tiny command evaluates Rust source code which read from the standard input stream.
It can handle extern crate
declaration and has simple caching mechanism.
Installation
Execute following command on your terminal:
# Installs `evalrs` command
$ cargo install evalrs
# Shows help message
$ evalrs -h
Usage Examples
evalrs
command reads Rust code snippet from the standard input stream and evaluates it:
$ echo 'println!("Hello World!")' | evalrs
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.daiPxHtjV2VR)
Finished debug [unoptimized + debuginfo] target(s) in 0.51 secs
Running `target\debug\evalrs_temp.exe`
Hello World!
If target code includes extern crate
declarations,
the latest version of those crates will be downloaded and cached:
# First time
$ echo 'extern crate num_cpus; println!("{} CPUs", num_cpus::get())' | evalrs
Updating registry `https://github.com/rust-lang/crates.io-index`
Compiling libc v0.2.18
Compiling num_cpus v1.2.0
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.HSRNyVQbM6s3)
Finished debug [unoptimized + debuginfo] target(s) in 0.55 secs
Running `target\debug\evalrs_temp.exe`
4 CPUs
# Second time (cached crates are used)
$ echo 'extern crate num_cpus; println!("{} CPUs", num_cpus::get())' | evalrs
Updating registry `https://github.com/rust-lang/crates.io-index`
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.4QzdqRG5cY0x)
Finished debug [unoptimized + debuginfo] target(s) in 0.24 secs
Running `target\debug\evalrs_temp.exe`
4 CPUs
If you want to use a specific version of an external crate,
you will be able to specify it at a trailing comment of the extern crate
declaration.
$ evalrs << EOS
extern crate num_cpus; // "1.2.0"
extern crate some_local_crate; // {path = "/path/to/some_local_crate"}
extern crate other_crate; // other-crate = "1"
println!("{} CPUs", num_cpus::get());
EOS
The command wraps input code snippet (except extern crate
declarations) with a main function.
But, if the code has a line which starts with "fn main()",
it will be passed to rustc
command without modification.
# The first execution is equivalent to the second.
$ evalrs << EOS
let a = 1;
let b = 2;
println!("a + b = {}", a + b);
EOS
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.gSXTXNaB6o8o)
Finished debug [unoptimized + debuginfo] target(s) in 0.53 secs
Running `target/debug/evalrs_temp`
a + b = 3
$ evalrs << EOS
fn main() {
let a = 1;
let b = 2;
println!("a + b = {}", a + b);
}
EOS
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.0kYvCRAj0TWI)
Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
Running `target/debug/evalrs_temp`
a + b = 3
To support documentation test format,
evalrs
removes "# " string at the beginning of a line.
$ evalrs << EOS
# fn main() {
let a = 1;
let b = 2;
println!("a + b = {}", a + b);
# }
EOS
Compiling evalrs_temp v0.0.0 (file:///tmp/evalrs_temp.0kYvCRAj0TWI)
Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
Running `target/debug/evalrs_temp`
a + b = 3
Emacs Integration
As an example of integration with Emacs,
you can use quickrun package
to evaluate Rust code in a buffer by using evalrs
command.
First, install quickrun
package as follows:
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(package-refresh-contents)
(package-install 'quickrun)
Next, add a quickrun command to execute evalrs
:
(quickrun-add-command
"evalrs"
'((:command . "evalrs")
(:exec . ("cat %s | %c %a")))
:default "evalrs")
Now, you can evaluate Rust code snippet in a buffer quickly:
extern crate num_cpus;
println!("You have {} CPU cores", num_cpus::get());
// Type following to evaluate this buffer:
//
// M-x quickrun RET evalrs
Dependencies
~3.5–5.5MB
~93K SLoC