Quick Answer: To rewrite an app in Rust without breaking it, port one unit at a time and check each piece against the recorded behavior of the original. rust-anything records what your TypeScript, Python, C, Go, Ruby or Java code returns, replays those cases against the Rust port, and blocks progress until every case matches.
Most full rewrites fail for a boring reason: nobody wrote down what the old code actually does. The team rebuilds features from memory, ships, and then discovers that the old parser trimmed whitespace, the old sort put "10" before "9", and the old function returned -0. Rewrites that stall usually stall here. The rust-anything project on GitHub takes a different route: record the original first, then let every Rust piece prove itself against that recording.
Why a big-bang rewrite keeps failing
A big-bang rewrite asks you to replace everything before anything can be checked. You get no partial signal, so bugs surface only after cutover.
Splitting the work doesn't solve it by itself. You also need the Rust code to behave like the original across the inputs that matter, and that is where most ports drift.
Performance is the other trap. Here's the part people underestimate: calling Rust once per small function is often slower than a JIT-compiled JavaScript or Python function. Every call pays for crossing the language boundary, converting arguments, and converting the result back. The gains come from moving the loop, not the function. In rust-anything's own race demo, typing user.profile into x18n's search over 50,000 keys took 143 ms in TypeScript and 6.9 ms through a Rust batch, with identical results at every keystroke. The batch version moved the whole loop across the boundary once.
That counter-intuitive result changes the order of work. Find the hot loop first, then port it.
Find the loop worth moving before you write any Rust
Start by measuring. Guessing where the time goes is how teams spend three weeks porting a function that accounts for 2% of runtime.
rust-anything's profile command runs a real workload under the language's own profiler (node, cProfile, go test pprof, JFR, or a Ruby tracer) and reports hot functions by inclusive time, their callers, and which loops are candidates for batching. It also flags code that is already compiled, such as numba, Cython or JAX, where Rust gains little.
A practical sequence looks like this:
- Run a representative workload, not a microbenchmark you invented. Use something close to what production does.
- Rank functions by inclusive time, then check who calls them. A cheap function called a million times from one loop is a batch candidate.
- Skip anything already compiled. Porting numba output to Rust is rarely worth it.
- Pick one unit with clear inputs and outputs, such as a pure function over strings or numbers, for the first port.
The analyze command goes further and lists units, port order, risks with file:line references, and a recommended bridge for each unit. Use it to decide what to port second, third, and so on.
Record what the original does, including the edge cases
This is the step that makes a rewrite safe, and it's the one most teams skip. rust-anything record runs your original code on generated edge cases: non-ASCII text, emoji, -0, NaN, integers where floats are expected, and more. It also covers semantic traps in each parameter, such as string length and case handling, signed division, overflow boundaries, float printing, and map and sort order. The results go into a cases.jsonl file.
Then rust-anything diff replays every recorded case against the Rust implementation, calling it the way the original's callers call it. Nothing ships until every case matches. The project's own benchmark suite reports 38,251 recorded cases replayed with 0 mismatches. That number is a claim about their suite, not a guarantee for your code, so treat it as a sign of how the process works rather than as proof for your app.
Be clear about the limit. A recording only catches what it covers. If no case exercises a particular error path, a bug there passes silently. This is why the gate requires a recorded failing case for every error path the original has, and why you should add your own real inputs to the recording. Recording is evidence, not a substitute for thinking.
For numeric code, floating-point results rarely match bit for bit after reordering operations. The --float-tolerance flag lets floats match within a stated relative tolerance, such as 1e-9. Set it deliberately and document why.
Bridge the Rust so callers don't change
Your rest-of-app code should keep calling the same API. rust-anything bridge generates a crate with the bindings and a stub file (port.rs) for you or your coding agent to fill in. The bridge depends on the source language:
- TypeScript/JavaScript: napi-rs or wasm-bindgen
- Python: PyO3, with abi3 for one wheel across Python versions
- C:
extern "C"functions with the original names and a header - Go: typed cgo calls
- Ruby: a magnus extension under the original module
- Java: typed JNI wrapper classes with the original methods
Callers keep the original import, function names and signatures. You can switch the implementation without touching the rest of the codebase.
Every language except C also supports batch classes through --batch. Items cross the boundary once and stay in Rust, and each call runs across all of them on all cores. This is where the speedups in the next section come from.
Use rust-anything shadow before full cutover. It runs the original and the Rust side together, returns the original's result, and reports mismatches. Set RUST_ANYTHING_MODE to old, shadow, or new to move traffic over in stages.
What the benchmarks actually show
Here are the figures the project publishes. Each one comes with a benchmark in the repo's demo/ folder, which checks that outputs match before timing.
| Workload (same results as original) | Language | Original | With rust-anything |
|---|---|---|---|
| x18n sidebar search, 50,000 keys | TypeScript (Node) | 10 ms per query | 0.5 ms (20x) |
slugify over 20,000 strings | Python | 98 ms | 17x faster |
Shellwords.shellescape, 50,000 values | Ruby | 88 ms | 11.8x faster |
Slugify over 50,000 strings | Go | 49 ms | 4x faster |
These numbers were measured on 4 cores. Your results depend on your data, your hardware, and how much of the runtime sits in the loop you moved. A 20x gain on one hot loop can translate to a few percent on the whole request path if that loop is only 5% of the time. Measure end to end before you celebrate.
Port a whole app, not just one hot path
Porting a single hot function is the easy case. Porting an entire app raises harder questions: which entry point owns the process, how dependencies map to Rust crates, and which units depend on each other and must move together.
rust-anything's analyze --whole-app covers that planning step. It identifies entry points, lists every dependency (imported or declared) with a candidate crate, groups tightly coupled units into pieces, and produces numbered decisions with a recommended answer for each. The idea is that Rust owns the entry point first, while the original answers the rest through a bridge. Each unit then goes through the same record, diff, and gate loop. When every unit passes, the final step is to remove the original.
Use this approach on a real app only after you have a working test suite and a recorded baseline. A front door that routes to Rust for some requests and the original for others is itself a risk, so keep the fallback rate visible and drive it down over time.
The gate is what decides a unit is done. rust-anything gate checks formatting, runs clippy with -D warnings, runs tests, audits unsafe usage, confirms nothing remains unported, and verifies every error path has a recorded case. It also flags unwrap, expect, or panic! where the original returned an error, unless you mark the line // INFALLIBLE: <why> with a real reason. That last check catches a common failure: a Rust port that panics on input the original handled gracefully.
Here's where most rewrites quietly go wrong: the team treats a passing gate as the finish line and never checks the unit under production traffic. Keep shadow mode running until the mismatch count stays at zero over real inputs, not only the recorded ones.
Frequently Asked Questions
What is rust-anything and how does it help me rewrite an app in Rust?
rust-anything is an open-source tool from erkamyaman on GitHub that records what your existing code does, builds a Rust port of one unit at a time, and checks the port against that recording. It also profiles hot paths, generates bindings, and provides a gate that decides when a unit is done. It does not translate code; your coding agent or you write the Rust.
How do I rewrite an app in Rust one piece at a time without breaking it?
Pick one unit with clear inputs and outputs, record the original's behavior on edge cases and your real inputs, write the Rust, and replay the recording until every case matches. Generate a bridge so callers keep the original API, run shadow mode in production, and move to the next unit only after the gate passes. Repeat until the app is pure Rust.
Why is my Rust port slower than the original code?
The most common cause is crossing the language boundary too often. A Rust function called once per small item can lose to a JIT-compiled function because argument conversion and the call itself cost more than the work. Move the loop instead, using a batch class so items cross once and stay in Rust. Profile first to confirm the hot path.
Can recorded tests guarantee a Rust rewrite behaves identically?
No. A recording only catches what it covers, so a bug on an input nobody recorded can pass unnoticed. Add your own real production inputs, cover every error path, and keep shadow mode running. Floating-point code may need an explicit tolerance because reordered operations rarely produce identical bits.
Start with one function this week
Run rust-anything profile on your slowest workload, pick the single loop that dominates the time, and record its current output on your real inputs before changing anything. Then port that one unit and check it with diff. Pass this to someone wrestling with a stalled rewrite, because the fastest way to get unstuck is one verified piece at a time.