Skip to content

用 Rust 重寫你的應用程式。並證明它的行為完全相同。

rust-anything 會錄製你的程式碼做了什麼,再將每個已錄製的案例拿來重播比對 Rust 移植版本。Rust 程式碼由你的 AI 代理撰寫。在輸出完全一致之前,什麼都不會出貨。

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 在純 Rust 執行檔上一致的 HTTP 案例
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,接著在 Claude Code、Codex、Cursor 與 Gemini CLI 中註冊 MCP 伺服器與工作流程技能。

$cargo install rust-anything
$rust-anything setup

macOS、Linux 與 Windows 的預先建置執行檔可在每個 GitHub release 下載。

一次移植如何完成

原始版本始終是唯一可信來源。rust-anything 不翻譯程式碼:它提供 AI 代理猜不到的資訊,以及一道判定移植何時完成的閘門。

  1. 錄製

    record

    record 會以自動產生的邊界案例、語意陷阱以及你自己的輸入執行原始程式,並儲存它回傳的值或擲出的例外。

  2. 移植

    bridge

    bridge 會產生 Rust crate 與包裝層,讓呼叫端繼續使用原本的 API。Rust 程式碼由你或你的 AI 代理撰寫。

  3. 檢查

    diff, gate

    diff 會將每個已錄製的案例拿來重播比對 Rust 版本,在全部一致且 gate 通過之前,什麼都不會出貨。

  4. 提升

    app lift

    當重播在沒有回退的情況下通過,且每個單元都通過 gate,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(或交給你的 AI 代理),然後:
$ 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 cases match
- fuzzyScore("b😀", "a😀b😀c"): expected {"ok":1997}, got {"ok":1998}

# JS 的 indexOf 以 UTF-16 程式碼單元計算,
# 所以一個 emoji 佔兩個位置。

# 使用自動產生的 utf16 輔助函式後:
# 2000/2000 個案例,且 gate 通過

在真實專案上實測

每個數字都有對應的基準測試,且會先確認結果完全相同。較慢的案例也一併列出:對每個小函式各呼叫一次 Rust,通常比 JIT 還慢。

json-server 基準測試 · 每秒請求數,第 1 次 / 第 2 次執行 · Apple M4,Node 24.15.0
請求(fixture 資料庫)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 是免費的開源軟體。如果它對你的工作有幫助,請考慮贊助。你的支持能讓開發持續下去。