Skip to content

用 Rust 重写你的应用。并证明它的行为完全一致。

rust-anything 录制你的代码的行为,然后在 Rust 移植版上回放每一条录制用例。Rust 代码由你的智能体编写。输出全部一致之前,什么都不会上线。

src/words.ts
export function wordCount(text: string): number {  return text.split(/\s+/).filter(Boolean).length}
src/port.rs桥接层: napi-rs
pub fn word_count(text: &str) -> usize {    text.split_whitespace().count()}
已验证的 TypeScript 移植版,Fuse.js:3,414 / 3,414 条录制用例全部一致
32x
Fuse.js 在 50,000 个字符串上的单次查询提速,Apple M4
3.5MB
json-server 收口后的空闲内存,Node 版为 74 MB
514/514
json-server 的 HTTP 用例在纯 Rust 二进制上全部一致
10
languages: eight with verified ports, Kotlin and PowerShell tested end to end
rust-anything on Trendshift (daily)rust-anything on Trendshift (daily?language=Rust)rust-anything on Trendshift (weekly)rust-anything on Trendshift (weekly?language=Rust)

Prettier, ported to Rust

Prettier and its Rust port format the same 59 real files, one process per file: Prettier takes 3.63 s, the Rust port 0.20 s, with the same output byte for byte.

Prettier and its Rust port format the same 59 files; the Rust port is 19x faster with identical output.

安装

安装 CLI,然后将 MCP 服务器和工作流技能注册到 Claude Code、Codex、Cursor 和 Gemini CLI。

$cargo install rust-anything
$rust-anything setup

每个 GitHub release 都附带 macOS、Linux 和 Windows 的预编译二进制文件。

一次迁移是如何完成的

原实现始终是唯一的事实来源。rust-anything 不翻译代码:它提供智能体无法靠猜测得到的东西,以及一个判断迁移何时完成的门禁。

  1. 录制

    record

    record 在生成的边界用例、语义陷阱和你自己的输入上运行原实现,并保存它的返回值或抛出的异常。

  2. 移植

    bridge

    bridge 生成 Rust crate 和一层包装,让调用方继续使用原有 API。Rust 代码由你或你的智能体编写。

  3. 校验

    diff, gate

    diff 在 Rust 实现上回放每一条录制用例,在全部一致且 gate 通过之前,什么都不会上线。

  4. 收口

    app lift

    当回放在零回退的情况下通过、且每个单元都通过门禁时,app lift 将其变为纯 Rust 应用。原实现始终不会被改动。

录制能抓住什么

x18n 的 fuzzyScore 的第一版移植按 Rust char 做索引。它通过了简单测试,却没能通过 diff。

~/x18n
# 时间花在哪里,以及谁在调用它
$ rust-anything profile . -- node bench.mjs
$ rust-anything bridge . src/fuzzy.ts --batch fuzzyScore:text
# 编写 src/port.rs(或交给智能体),然后:
$ rust-anything record rust/fuzzy-rs
$ rust-anything build rust/fuzzy-rs
$ rust-anything diff rust/fuzzy-rs
$ rust-anything bench rust/fuzzy-rs
$ rust-anything gate rust/fuzzy-rs
# diff,第一版移植
198/200 条用例一致
- fuzzyScore("b😀", "a😀b😀c"): expected {"ok":1997}, got {"ok":1998}

# JS 的 indexOf 按 UTF-16 码元计数,
# 所以一个 emoji 占两个位置。

# 使用生成的 utf16 辅助函数后:
# 2000/2000 条用例一致,门禁通过

在真实项目上实测

每个数字都有对应的基准测试,并且会先校验结果完全一致。慢的情况也一并列出:每个小函数调用一次 Rust,通常比 JIT 更慢。

Orange rows are where Rust gains little or loses: the honest cases, shown on purpose.

json-server 基准测试 · 每秒请求数,第 1 次 / 第 2 次运行 · Apple M4,Node 24.15.0
请求(测试夹具数据库)Node,1 连接Rust,1 连接Node,50 连接Rust,50 连接
GET /posts11.1k / 10.8k27.3k / 23.9k19.7k / 20.4k115.0k / 110.7k
GET /posts/111.0k / 11.3k27.7k / 25.4k21.9k / 22.6k119.9k / 99.0k
?_embed=comments8.7k / 6.6k22.5k / 21.8k13.8k / 11.0k98.3k / 100.6k
POST /comments,10 个连接2,973 / 2,4675,821 / 5,976

差距较小的地方:单个连接读取完整的 10,000 条帖子列表(1.2x),以及大数据库上的写入(1.1 到 1.2x),因为两者每次写入都会重写整个文件。完整表格

赞助商

rust-anything 免费且开源。如果它对你的工作有帮助,欢迎考虑赞助。你的支持让开发得以持续。