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.
Execute following command on your terminal:
```bash
evalrs command$ cargo install evalrs
$ evalrs -h ```
evalrs command reads Rust code snippet from the standard input stream and evaluates it:
bash
$ 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:
```bash
$ echo 'extern crate numcpus; println!("{} CPUs", numcpus::get())' | evalrs
Updating registry https://github.com/rust-lang/crates.io-index
Compiling libc v0.2.18
Compiling numcpus v1.2.0
Compiling evalrstemp 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
$ echo 'extern crate numcpus; println!("{} CPUs", numcpus::get())' | evalrs
Updating registry https://github.com/rust-lang/crates.io-index
Compiling evalrstemp v0.0.0 (file:///tmp/evalrstemp.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.
```bash $ evalrs << EOS extern crate numcpus; // "1.2.0" extern crate somelocalcrate; // {path = "/path/to/somelocalcrate"} extern crate othercrate; // 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.
```bash
$ evalrs << EOS
let a = 1;
let b = 2;
println!("a + b = {}", a + b);
EOS
Compiling evalrstemp v0.0.0 (file:///tmp/evalrstemp.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 evalrstemp v0.0.0 (file:///tmp/evalrstemp.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.
```bash $ evalrs << EOS
let a = 1; let b = 2; println!("a + b = {}", a + b);
EOS
Compiling evalrstemp v0.0.0 (file:///tmp/evalrstemp.0kYvCRAj0TWI)
Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
Running target/debug/evalrs_temp
a + b = 3
```
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:
lisp
(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:
lisp
(quickrun-add-command
"evalrs"
'((:command . "evalrs")
(:exec . ("cat %s | %c %a")))
:default "evalrs")
Now, you can evaluate Rust code snippet in a buffer quickly:
```rust extern crate num_cpus;
println!("You have {} CPU cores", num_cpus::get());
// Type following to evaluate this buffer: // // M-x quickrun RET evalrs ```