13 个版本

0.0.13 2022 年 11 月 22 日
0.0.12 2020 年 10 月 18 日
0.0.11 2018 年 7 月 26 日
0.0.10 2018 年 1 月 27 日
0.0.6 2016 年 12 月 12 日

#295 in 开发工具

MIT 许可证

12KB
161

evalrs

evalrs License: MIT

Rust 代码片段评估器。

这个小巧的命令可以从标准输入流读取 Rust 源代码并对其进行评估。

它可以处理 extern crate 声明,并具有简单的缓存机制。

文档

安装

在您的终端上执行以下命令

# Installs `evalrs` command
$ cargo install evalrs

# Shows help message
$ evalrs -h

用法示例

evalrs 命令从标准输入流读取 Rust 代码片段并进行评估

$ 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!

如果目标代码包含 extern crate 声明,则会下载并缓存这些包的最新版本

# 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

如果您想使用外部包的特定版本,可以在 extern crate 声明的末尾注释中指定。

$ 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

命令将输入代码片段(除了 extern crate 声明)包裹在主函数中。但是,如果代码中有一行以 "fn main()" 开头,则将不会对其进行修改并传递给 rustc 命令。

# 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

为了支持文档测试格式,evalrs 会移除行首的 "# " 字符串。

$ 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 集成

以下是一个与 Emacs 集成的示例,您可以使用 quickrun 包通过使用 evalrs 命令在缓冲区中评估 Rust 代码。

首先,按照以下步骤安装 quickrun

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(package-refresh-contents)
(package-install 'quickrun)

然后,添加一个 quickrun 命令以执行 evalrs

(quickrun-add-command
 "evalrs"
 '((:command . "evalrs")
   (:exec . ("cat %s | %c %a")))
 :default "evalrs")

现在,您可以在缓冲区中快速评估 Rust 代码片段

extern crate num_cpus;

println!("You have {} CPU cores", num_cpus::get());

// Type following to evaluate this buffer:
//
// M-x quickrun RET evalrs

依赖关系

~3.5–5.5MB
~94K SLoC