Write it in Python, ship it in Rust
Python is still the easiest way to do science on a computer. Rust is how I get that science to other people, and I think the rest of the field is quietly making the same split.
- Published
- 29 November 2026
- Read
- 7 min
In 2019 I published pytrax with colleagues. It sends random walkers through an image of a porous material and measures how much the structure slows them down. It’s about 600 lines of Python and leans on numpy. Python made it easy to write, and that is still the main reason most science is written in Python.
This year I rebuilt it in Rust as gtrax. On the same images, with the same walkers and the same number of steps, pytrax took 78 seconds and gtrax took 12, and the 12 includes Rust’s build tool starting up. The numbers agree to within 1%. In the browser, the whole gtrax engine is about 250 KB.
I started writing Python during my PhD in 2013. I’m swapping to Rust now because I don’t need to understand the code to use it.
This post is my view on where the two languages sit for science and machine learning right now. In short, Python is not being replaced. It’s being hollowed out underneath, one hot loop at a time, and WebAssembly is the part of that story that I think deserves more attention. It’s for people who write research code in Python and wonder whether Rust is worth the trouble. You don’t need to know any Rust.
Why Python won
Python dominates science because it’s easy to read, easy to try things in, and someone has nearly always written the package already. Six of the nine projects I’ve rebuilt so far started life as Python packages: elapid for species maps, systole for heart rate variability, PySINDy for discovering equations from data, PoreSpy and pytrax for porous materials, and Jaxley for neurons. The other three are in R, C++ and Rust.
None of those authors chose Python for speed. They chose it because a PhD student can go from an idea to a plot in an afternoon, in a notebook, with the data right there. That matters more than anything else when you don’t yet know what you’re looking for, which in research is most of the time.
The numbers still back it up. Python overtook JavaScript as the most used language on GitHub in 2024. TypeScript overtook both in August 2025, but by GitHub’s count Python was still the main language of nearly half of all new AI projects. In machine learning there isn’t really a second choice for the code people write themselves.
The fast parts were never Python
However, the parts of the scientific Python stack that do the heavy lifting were never written in Python. numpy is C. PyTorch is C++ and CUDA with a Python front end. scipy is a mix of C, C++ and Fortran. Python is the language you hold the tools in, not the one they’re made of.
What’s changed in the last few years is the language underneath. New tools are being written in Rust rather than C:
- Polars, a dataframe library, is built on a Rust query engine.
- Pydantic moved its validation core to Rust in version 2.
- Ruff, a linter and formatter, and uv, a package manager, are both Rust.
- Hugging Face’s tokenizers and safetensors are both Rust under a Python interface.
JetBrains’ State of Python survey for 2025 found the share of Python developers using Rust for binary extensions went from 27% to 33% in a year. On the Stack Overflow survey Rust has been the most admired language for years running, at 72% in 2025.
So when people say Python is being replaced, I don’t think that’s quite right. You still type import polars. You still pip install it, or more likely now uv install it. The surface stays Python and the engine room is moving to Rust. I think that’s a sensible split, and it’s close to what I’ve ended up doing.
The naive way: run Python in the browser
The thing I actually wanted was for anyone to be able to run pytrax without installing anything. No conda, no virtual environment, no “works on my machine”. Just a link.
The obvious way to do that is Pyodide, which is CPython compiled to WebAssembly so it runs in a browser tab. It works, and it’s an impressive project. But pytrax needs numpy and scipy, so before a single walker moves the browser has to fetch roughly 6 MB for Python itself, 3 MB for numpy and 13 to 16 MB for scipy. That’s 20 MB or more, plus a few seconds to start the interpreter, to run a few hundred lines of maths.
| Download before it runs | |
|---|---|
| pytrax in the browser, via Pyodide | about 20 MB or more |
| gtrax, compiled from Rust | about 250 KB |
| all eleven modlit apps’ engines together | about 2.5 MB |
That’s about 90 times smaller for the same job, and the reason is simple. The Rust version only ships the code it uses. There’s no interpreter, no standard library and no scipy, only the walker and the few functions it calls.
The same code, in a browser tab
The Rust code in gtrax is one crate. I run its tests natively with cargo test, I ran the pytrax comparison natively, and then exactly the same code compiles to WebAssembly and runs in your browser. There isn’t a JavaScript port to keep in step, or a second version that might drift.

It goes further when the original is already in Rust. ions is RustBCA, a code for ions hitting surfaces from the University of Illinois, compiled to WebAssembly without changing a line. It gives the same counts as the original on 16 reference runs, because it is the original.
A few other things come with it:
- Nothing leaves your machine. When you load your own scan into gtrax, it’s read by the page. There’s no server to send it to.
- It doesn’t cost anything to run. The site is static files. A thousand people running a simulation costs the same as one.
- It will still work in ten years. A Python environment from 2019 can be a pain to rebuild today. A WebAssembly file from today should keep running as long as browsers do.
What Rust costs
It isn’t all good, and I’d be wary of anyone who says it is.
Rust is slower to write, especially at the start. The compiler is strict about which part of the code owns which piece of data, which catches real bugs but makes the first few weeks slow going. There’s no real equivalent of a notebook, so the “try it and plot it” loop that makes Python so good for research is much clunkier. Builds take longer than hitting run.
The bigger cost for science is the ecosystem. There’s no scipy for Rust. When I rebuilt elapid as range, elapid fits its model with glmnet, and there wasn’t a Rust version I could drop in, so range has its own fitting step. It agrees with elapid’s maps to a correlation of 0.996 or better, but it had to be written and checked from scratch, where in Python it would have been one import.
So I wouldn’t tell a PhD student to start their next project in Rust. I’d tell them to write it in Python, find out what it is, and then think about Rust for the part that has to be fast, or the part other people need to run.
Python for asking, Rust for answering
The way I think about it now is that Python is the language for asking questions and Rust is the language for answering them for other people. While you’re still finding out what the model should be, you want Python’s speed of thought. Once you know, and you want it fast, small, correct and runnable by anyone with a browser, Rust and WebAssembly are hard to beat.
That’s also roughly what the big projects are doing, keeping the Python people know and swapping what’s underneath. The difference with WebAssembly is that the engine doesn’t have to stay underneath a Python install at all. It can go straight to the reader.
Back to pytrax. I still think it’s a good piece of Python, and it’s still the reference I check gtrax against. It’s just that today, if you want to see what it does, you don’t need to install it. You can open gtrax and watch the walkers go, and that took one rebuild in Rust.