Translating code to Rust

Part 5: Scaling up

Introduction to agentic AI

About this lecture

Research software is often slow, hard to install, or both. A Python script that needs the right packages, a pipeline that only runs inside Docker, a tool that will not run on Windows. Agents can now translate such code into Rust, a fast, compiled, memory-safe language, and give you one file that simply runs. But only if you prove that the translation gives the same answers.

  • Why Rust: speed, one self-contained program, no Conda or Docker, and possibly fewer security holes.
  • How: reference outputs first, static analysis to guide the agent, one function in and one function out, bottom-up.
  • Proof: identical outputs on many inputs, then an honest benchmark.
  • Judgement: when translating is not worth it, and what the licence of the original allows.

A workflow unit that builds directly on Reading other people’s code. It draws on a 2026 study by the author of this course.

The QC script works, but half the class cannot run it

Three machines. A Windows laptop where python is not recognised, conda fails to solve the environment and Docker Desktop needs admin rights. An Apple laptop where a Docker image for another processor runs under slow emulation. An HPC cluster where docker is not available. Below: what we want, one file per system that runs without Python, Conda or Docker.

The same script also takes hours on a full sequencing run.

Containers and Conda solve installation by shipping a whole computer

Left, a container image holding operating system files, a Conda environment with many tools, and your pipeline; the tools talk through files and pipes. Right, after translation, one Rust binary holding your pipeline and the translated tools, called as functions; Cargo manages versions at build time.

Rust is a compiled language: you build once, then run one file

Top: a Python script needs the interpreter and packages on every computer and runs line by line. Bottom: main.rs is built once with cargo build --release into fastq_stats.exe, one file to copy and run. The compiler checks types and memory use before the program runs; one binary per system.

One binary per system makes teaching and sharing simpler

  • Student laptops: download one file and run it. No Python, Conda or Docker, no admin rights.
  • Windows: a native .exe, not a Linux tool inside WSL or a container.
  • Apple and Linux: one binary each; Cargo can build for other systems and processors (cross-compiling).
  • HPC clusters: copy the binary into your home folder. No container runtime, no modules.
  • Archiving: the binary and its source, with exact library versions in Cargo.lock, rebuild the same program years later.

Cargo lets two versions of a library live side by side

  • In Python or R, one environment holds one version of each package. Two tools that need different versions conflict, so you make separate environments.
  • Cargo builds each program with exactly the versions it asks for, and can include two versions of the same library in one program.
  • The paper’s claim: after translating Bascet’s upstream tools, “version management is now solely managed by the Rust package manager Cargo”, removing the need for Conda.

Compiled languages use far less time and energy on the same task

Bar chart, log scale. Energy relative to C: C 1.00, Rust 1.03, C++ 1.34, Java 1.98, Python 75.88.

A later analysis (van Kempen et al. 2025): power draw is similar across languages, so energy mostly follows run time.

The paper’s case: legacy code costs money, energy and hardware

  • Maintenance (technical debt): tools without maintainers, outdated libraries, hard to combine R and Python.
  • Energy: R, Python and MATLAB can use “>50x more energy than compiler or semi-compiled languages for certain workloads”.
  • Hardware: computers are becoming scarce, and new hardware needs software rewritten anyway.
  • Safety: legacy code “may also have security or safety problems that make it unsuited for use in clinical settings”.
  • People: a bioinformatician is expected to know R, Python, JavaScript and C or C++. One language for the “full stack” would help.

Rust rules out a class of memory bugs, not wrong science

Left, ruled out by the compiler in safe Rust: use after free, reading past the end of an array (the program stops), data races between threads, unchecked null values, type mix-ups. Right, still possible: wrong formula or threshold, translation errors, code marked unsafe, buggy or malicious dependencies, memory leaks.

Fewer dependencies is also a security gain, until updates are automated

  • A container image carries an operating system and hundreds of packages, each a possible way in.
  • The paper’s group counted roughly 2,000 to 3,500 transitive packages behind the 61 microbial tools they surveyed (a qualitative estimate made with Codex).
  • A Rust binary contains only the crates it was built with, listed with versions in Cargo.lock; cargo audit checks them against known vulnerabilities.
  • But: auto-updating many translations with agents would invite supply-chain attacks, as seen in the JavaScript ecosystem. The paper suggests “a more conservative approach to updating”.

A 2026 study translated about 35 research tools with agents

  • Henriksson J. Static analysis-guided agentic AI translation enables Rust as a full stack bioinformatics language. arXiv:2608.13029 (2026), CC BY 4.0.
  • About 40 attempts, 35 packages in sequencing (NGS), imaging and file-format libraries, “tested, benchmarked, and ready for use by early adopters”. Translation bugs are still expected.
  • 11 weeks, Claude Code and Codex on three subscriptions, using up to about 90 % of their limits.
  • From days to weeks per tool: jpegxr (31,000 lines of C++) in under 20 hours; BWA-MEM2 took weeks.
  • Code, prompts and tools: one GitHub repository per tool; updates at github.com/henriksson-lab/rustification.

Naive prompts gave code that compiled, ran, and was wrong

The paper lists errors that agents made again and again:

  • swapped expressions (a<b became b>a) or reordered branches
  • wrong number types, causing overflows
  • changed order of operations, so decimals came out different
  • clever algorithms replaced with naive ones
  • the wrong one of several similar functions called
  • new “helper” functions that bent the design out of shape
  • speed tricks (SIMD) left out

Asking more firmly did not fix it: such prompts were “ineffective for Claude, and mostly insufficient for Codex”.

Rule one: one original function becomes one Rust function

Four original C functions such as ParseHeader map one to one to Rust functions such as parse_header. A new helper function adapt_input on the Rust side is crossed out: treated as a bug.

Same files and folders as the original; stubs for every function are written first.

Static analysis gives the agent a map: translate from the bottom up

Call graph: main calls parse_args and run_align; run_align calls load_index and align_reads; these call decode_hdf5 and score_chain. Numbers show translation order: leaves 1, then 2, then run_align 3, main 4. Same-numbered pieces can go to parallel agents.

Three tools check the translation function by function

  • CCC (code complexity comparator): compares call graphs and, per function, the types, numbers, strings, branches and logic. A function with fewer * or fewer branches than its original has probably lost something.
  • tracehash: records a fingerprint of every function’s inputs and outputs in both programs and finds the first call that differs.
  • gdbtv: runs original and translation side by side in two debuggers and stops where they part ways. Neither agent reached for it on its own.

All three were “vibe coded” with Claude, and the agents learn to use them from instructions in their repositories.

A fingerprint of every function call shows where the translation goes wrong

Table of function calls with input and output hashes for original and Rust. Three calls match. The second score_pair call has the same input hash fe11 but output 4422 in the original and 4499 in Rust: the bug is inside score_pair.

The paper’s workflow: correctness first, speed later

Seven steps: 1 scaffold with static analysis and stubs, 2 translate all of it bottom-up, 3 test on synthetic data versus original, 4 test on real data versus original, 5 benchmark and speed up, 6 clean up, 7 add a Rust API. Differences in testing send you back to step 2.

Equivalence is tested on outputs, on real data, many times over

  • Compare the translation’s output with the original’s on many varied real datasets. The paper: tests written by the agents themselves were “largely insufficient”.
  • Audit file by file: every file listed in TOAUDIT.md must pass two consecutive audits without complaints.
  • Even then: systematic audits “did not remove all errors, as revealed by testing on real data”.
  • Bonus: the translation found at least 3 bugs in the originals (an outdated broken dependency, a use-after-free, an unhandled corner case).

Decimal numbers depend on the order of operations

(0.1 + 0.2) + 0.3 equals 0.6000000000000001 while 0.1 + (0.2 + 0.3) equals 0.6. Illustrative example: the original gives p = 0.04998 and the gene is in the list; the translation gives p = 0.05001 and the gene is dropped.

The paper: agents “frequently did not respect order of operations”; translating HMMER was hard for this reason.

Most translations matched or beat the original’s speed

Bar chart of speed-up for 30 translated tools on a log scale. Largest: bioformats-rs 44.43x (Java), openslide-pure-rs 9.34x, circos-rs 7.43x (Perl), deconwolf-rs 3.32x. Many C and C++ tools between 0.9x and 1.8x. Slowest: vtk-pure-rs 0.77x and mdbtools-pure-rs 0.59x.

Memory use went down for most tools, and up for some

Bar chart of peak memory, Rust divided by original, for 28 tools. Highest: clic-rs 1.61, hmmer-pure-rs 1.60, deconwolf-rs 1.48. Lowest: fastqc-compliant-rs 0.18, bioformats-rs 0.15, vtk-pure-rs 0.13.

Bascet: one binary instead of a container, and native Windows

Bascet results: about 80 times smaller to distribute (200 MB binary instead of a container), about 10 times faster to build (20 minutes down to 1 to 2 minutes), more than 3 times faster key steps (SKESA and GECCO used as libraries), runs on native Windows without WSL2 or container, native Apple M3 binaries by cross-compiling.

The paper: “the only single-cell pipeline able to run on native Windows, without a container”.

The big speed-up comes from using tools as libraries, not commands

Top: a tool called once per cell repeats start program, load database, read input, process, write output for every cell. Bottom: a translated tool used as a library loads once, then only processes cells, with data passed in memory. 100 million cells times 1 second start-up equals 1,157 days.

Not everything could be translated

  • BLAST failed twice. Code that generates code (build-time generation, C++ templates, heavy preprocessor use) breaks the one-function-in, one-function-out idea.
  • C translates to unsafe Rust first: pointers and manual memory management, so no safety guarantees until a separate clean-up pass.
  • Memory sometimes went up, and reducing it would cost faithfulness or safety.
  • Speed tricks that reorder arithmetic (SIMD) could not be used without changing results.
  • Graphical interfaces were translated loosely, from screenshots, not faithfully.

The paper’s own caveats

  • Benchmarks are “best-effort estimates”, chosen by the agents with some steering, and differ per tool.
  • The prompts may work less well for others: the agents “picked up implicit assumptions based on our workspaces”. “Further independent testing is needed.”
  • Translations are for early adopters; new bugs are introduced as well as old ones fixed.
  • Agent-written tests were largely insufficient; real data had to do the work.
  • It is a single-author preprint, with models that changed during the study. It is not a comparison of models.

In the author’s experience, Codex translated more conservatively

  • Codex (GPT-5.4 and 5.5) for the first translation: “more conservative” in translating functions verbatim and in following instructions.
  • Claude (Opus 4.7) for difficult bugs that Codex could not find, for optimisation ideas, and for all graphical interfaces.
  • For benchmarks Codex was used, because Claude “frequently generated biased benchmarks”.
  • Claude starts parallel subagents on its own when the work suits it; Codex needed explicit prompting.

One person’s experience with one generation of models. Test on your own task (Choosing a model).

A benchmark is only fair if both programs do the same work

Left, a fair comparison: identical outputs first, same machine, same real input of realistic size, same thread count, both optimised, warm-up then several runs with the median, time and peak memory. Right, ways a benchmark flatters: 8 Rust threads versus 1 C thread, original without optimisation, tiny input, SIMD disabled, cold versus warm runs, Java not warmed up, faster because it skips work.

The workflow with your agent extends the recipe from the last unit

Workflow: original locked as the specification, reference outputs on many inputs and edge cases, scaffold with map, call graph and one stub per function, translate bottom-up faithfully, compare exactly on every reference input (differences go back to translate), benchmark fairly for time and memory, release binary with README, licence and credit.

Golden files and property tests catch what a single example misses

Many inputs, real data, edge cases, 100 generated random files and every option, go into both the original and the Rust port; the outputs, error messages and exit codes are compared byte by byte. Golden files are saved outputs of the original; a property test checks that for any generated input both give the same answer.

Translate when it pays off, and when you can prove it is the same

Decision chart. Is it slow, run often, or hard to install for others? If no, leave it. Can you get reference outputs on real inputs? If no, get them first. Does the licence allow a derived version, with credit planned? If no, ask the authors first. Is the original stable? If stable, translate with the workflow; if changing fast, wait or ask the authors to move together.

A translation is a derived work: respect the licence and give credit

  • Licence: keep the original’s licence file and terms. No licence means all rights reserved: ask first.
  • Credit: cite the original authors and paper in the README, with the exact version (git hash) translated.
  • Say what it is: a different name (the paper adds -rs), a clear note that it is an AI-assisted translation, version 0.x.
  • Courtesy: tell the original authors; send bug reports about the translation to your repository, not to theirs.
  • Reproducibility: faithful means keeping the original’s logic bugs, so results stay comparable across studies.

A faithful translation can follow its original’s updates

  • The paper updated two translations (openslide, minibwa) to newer upstream versions in under an hour each, because one-to-one translation keeps every change in a known place.
  • “Every line of code is a liability”: maintaining a translation adds less than writing a competing new tool.
  • Translation also clarifies provenance: every Rust function points back to an original one, unlike code an agent writes from scratch out of unknown sources.

In our test run of the exercise, the port was identical and about 19 times faster

Terminal session, illustrative. Comparison of Python and Rust outputs on reads.fastq, tiny.fastq and five edge cases: all identical, including error message and exit code. Benchmark on 1 million synthetic reads: Python about 165 seconds and 13 MB, Rust about 9 seconds and 3 MB.

What the agent does and does not do here

It does well

  • writes Rust you could not write yourself
  • installs Rust and builds release binaries
  • writes comparison scripts and generates test inputs
  • finds and fixes differences, if pointed at them
  • runs benchmarks, when told what fair means

It does not

  • stay faithful without constant reminders
  • choose real, varied test data on its own
  • benchmark fairly without being asked
  • know whether the licence allows the port
  • decide whether the port is worth maintaining

Summary

Concept Key fact
Rust compiled, about as fast as C, memory-safe in ordinary code; Cargo builds and manages libraries
One binary no Python, Conda or Docker on the machine that runs it; native Windows, macOS, Linux, clusters
Security removes memory bugs, not logic bugs; unsafe code and dependencies still matter
Energy roughly follows run time; interpreted code can need tens of times more
Faithful translation one function in, one function out, no helpers, stubs first, bottom-up from the call graph

Summary (continued)

Concept Key fact
Equivalence identical outputs, error messages and exit codes on real data, edge cases and generated inputs
Benchmark outputs identical first; same machine, input and threads; warm-up, several runs, median; time and memory
The paper ~35 tools; Bascet ~80x smaller, ~10x faster build, >3x faster key steps, native Windows; read as the paper’s claims
When not to one-off scripts, code you cannot test, fast-moving upstream, licence that does not allow it
Credit a translation is a derived work: keep the licence, cite the authors, name it clearly

Next (Using agents to train AI: curated and simulated data): how agents audit labelled data and write simulators that make training data with exact labels, and why a small real test set decides whether any of it works.