Python to Rust
Python to Rust converter that keeps Python's behaviour.
Paste typed Python functions and get Rust you can import from Python (PyO3). It runs in your browser, so your code never leaves your computer. Anything it can't convert is listed with the reason, never guessed.
Press Convert to Rust (or Ctrl+Enter). Typed functions on numbers, str, list, dict, set, tuple and Optional are converted; anything else is listed with the reason.
Same answers, not just the same syntax.
Line-by-line translators give you Rust that compiles and quietly means something else. This one keeps the places Python and Rust disagree.
Floor division
-7 // 2 is -4 in Python and -3 in plain Rust. The converted code gives -4, and % follows the same rule.
Text as characters
len("café") is 4, not 5 bytes. Indexing, slicing and loops over a str work on characters, as in Python.
Dicts keep their order
A dict comes back in insertion order, the same as Python's, so code that iterates the result still works.
Python's errors
A bad index raises IndexError, a missing key KeyError, division by zero ZeroDivisionError, and your own raise ValueError(...) is kept.
Negative indexes and slices
xs[-1], s[1:-1] and s[::-1] mean what they mean in Python.
round() and overflow
round() ties to even and returns an int. Ints are 64-bit, and an overflow raises OverflowError instead of quietly wrapping.
What it converts, and what it leaves in Python.
The hot loops of most Python code are plain functions on numbers, text and collections. That is what it converts.
Converts
- Typed functions: int, float, bool, str, list, dict, set, tuple and Optional
- if / elif / else, for, while, break and continue
- range, enumerate, zip, reversed and sorted
- List, dict and set comprehensions
- Common str, list and dict methods
- f-strings, math, min/max/sum/any/all and recursion
- Calls between the functions in your code
Leaves in Python, with the reason
- File, network and other I/O
- Classes and methods
- try / except, with, generators and async code
- NumPy, pandas and other libraries
- Functions without type hints
- eval, exec, getattr and other dynamic features
Tested against real Rust, input by input.
The converter is checked on 32 functions covering everything above. Each is converted, built with the Rust compiler and run beside the Python original on hundreds of inputs, including the edge cases where the languages disagree. All 32 match.
6.9× faster
Edit distance between strings, in our tests.
2.6× faster
Counting 20,000 numbers into buckets.
Small inputs, small gains
Moving data between Python and Rust has a cost. Convert the functions that do real work per call.
Converting is free. The ready-to-build download is part of FNJ Lifetime Unlimited, $29 once.
Python to Rust questions. Answered.
If something doesn't work as expected, let us know. A person answers.
Is my code uploaded?
No. The converter runs in your browser: the page loads Python (compiled to WebAssembly) and converts your code on your computer. Nothing you paste is sent to FNJ.
Will the Rust give the same results as my Python?
That is what it's built for. It keeps Python's behaviour where Rust differs by default, and we test it on 32 functions covering everything it converts: each is built with real Rust and must return exactly what Python returns, on hundreds of inputs and the functions' own tests. Two differences are by design: ints are 64-bit (an overflow raises OverflowError), and a set or dict key can't be a float. Run your own tests against the Rust before you switch.
What can't it convert?
I/O, classes and methods, try/except, with, generators, async code, NumPy and other libraries, untyped functions and dynamic features like eval. It never guesses: each function it can't convert is listed with the line and the reason, and stays in Python.
How much faster is the Rust?
It depends on the work. In our tests an edit-distance function ran 6.9 times faster, and counting 20,000 numbers into buckets 2.6 times faster. Very small inputs gain little, because moving the data between Python and Rust costs about as much as the work itself.
How do I use the result?
Download the project, then run pip install maturin and maturin develop --release in your project's virtual environment. You can then import the functions from Python like any module. You need Rust installed (rustup.rs). The download is part of FNJ Lifetime Unlimited.
What does it cost?
Converting and reading the Rust is free, with no sign-in. Downloading the ready-to-build project needs FNJ Lifetime Unlimited, $29 paid once, which also covers FNJ's Framer converters.