For a Python team, Rust vs Python isn't a language switch. Keep Python as the language of the product, profile it, and move only the function the profiler blames into a Rust extension built with PyO3 and maturin. Rewrite a whole service in Rust only when the hot path is the service.
The short answer: stay in Python, extend with Rust, or rewrite?
Start from what you already own and where the time actually goes.
| Situation | Move | Why |
|---|---|---|
| New CPU-bound component, no Python dependency | Rust (or Go) | Nothing to integrate with, so no boundary to maintain |
| One hot function in an existing Python service | PyO3 extension | Smallest Rust that fixes the problem |
| Slow because of I/O waits or architecture | Stay in Python, profile | Rust won't make a database answer faster |
| Data and ML work | Stay in Python | The libraries are Python; Rust sits underneath them |
If the first row describes you and the component is systems-style code, Rust vs C++ and memory-safety guidance covers the native-code side.
Is Rust replacing Python?
No. Stack Overflow's 2025 developer survey asked which languages people had done extensive work in over the past year. Among all 31,771 respondents who answered, 57.9% said Python and 14.8% said Rust. Among the 24,759 professional developers it was 54.8% and 14.5%. Python also gained 7 percentage points on the year before.
So Rust is a real second language for a minority, and Python is still growing. The survey is self-selected and measures recent work, not job openings, so read it as pool size.
The more useful evidence is what the big Python tools do. Ruff's documentation calls it an extremely fast Python linter and code formatter written in Rust, and says it replaces Flake8, Black, isort and more. Pydantic's docs say its core validation logic is written in Rust. You write Python against both. The Rust sits behind the interface, which is the pattern we'd copy.
Where does Rust beat Python, and where doesn't it?
Rust wins where the time goes into your own code: tight CPU-bound loops, parsing, scoring, and memory-heavy data structures. It also runs native threads without a global interpreter lock, which suits work you can split across cores.
It doesn't help when the service waits on a database or an upstream API, when the code is glue between libraries, or when the work lives in a notebook. A faster function behind a slow query is still a slow request.
How much faster, for your code? Ruff's "10-100x faster than existing linters (like Flake8) and formatters (like Black)" is a vendor claim about linting only. Benchmark posts that print a single figure with no method tell you nothing about your code. Time your own function, as the example below does.
How do you add Rust to a Python project?
What PyO3 and maturin are
The PyO3 guide describes PyO3 as "Rust bindings for Python, including tools for creating native Python extension modules." maturin is the build tool that compiles that Rust into a module Python can import, and packages it as a wheel. PyO3 needs Rust 1.85 or newer and CPython 3.9 or newer.
The path, five steps
- Profile and pick one pure function: no I/O, no global state, simple inputs and outputs.
- Create a virtual environment and install maturin:
python3 -m venv .venv, thenpip install -U pip maturin. - Scaffold the project with
maturin new -b pyo3 <name>, which creates the project in one command. The PyO3 guide's quickstart usesmaturin initand selects pyo3 when prompted. Expect aCargo.tomland asrc/lib.rs; maturin's layout also needs apyproject.toml. - Write the function in Rust. In
Cargo.toml, the[lib]section needscrate-type = ["cdylib"], and the module name insrc/lib.rsmust match itsname. - Run
maturin develop --release, import the module and time it against the Python original. Ship withmaturin build, which produces a wheel.
The PyO3 docs' example looks like this:
#[pyo3::pymodule]
mod string_sum {
use pyo3::prelude::*;
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
}Skip --release and you'll time an unoptimised build. The maturin tutorial describes the flag as the one "for speed testing", and maturin develop installs into your current environment without generating a wheel.
What it costs you
You add the Rust toolchain to CI, and you build a wheel for every platform you ship to. You also get a second language in code review. The ramp-up for a Python-only team is real, so plan for a named owner of the extension.
The other cost is the boundary. Every call crosses from Python into Rust, and in our experience data gets converted or copied on the way. A function that does a lot of work on a little data benefits. A function called a million times on tiny inputs may not.
Does free-threaded Python change the argument?
A little. Python 3.14, released on 7 October 2025, made the free-threaded build officially supported (PEP 779), at a single-thread cost of roughly 5-10% depending on platform and compiler. That helps thread-parallel Python code but doesn't turn a hot loop into native code, and free-threaded Python 3.14 versus Go covers the rest.
Rust vs Python side by side
| Criterion | Rust | Python |
|---|---|---|
| Typical job | Hot paths, native components, tooling | Products, data, ML, glue |
| Speed model | Compiled | Interpreted; extensions for hot paths |
| Memory model | Ownership checked at compile time | Garbage collected |
| Packaging | Cargo; wheels via maturin | pip and wheels |
| Learning curve | Steep | Gentle |
| Free-threading | Native threads | Supported build since 3.14 |
| Best for | The few functions that must be fast | Everything around them |
Worked example: one hot function in a Python service
Take a 6-engineer team running a Python backend. A profiler shows one CPU-bound pure function, say a scoring loop, takes most of each request's time.
There are three moves. Optimise in Python first, since a better algorithm or a vectorised library call costs nothing in tooling. If that isn't enough, write that one function as a PyO3 extension. Rewriting the service in Rust is the third option, and only worth it when that hot path is most of the service.
For the extension, run the five steps above. The commands below follow the PyO3 guide's quickstart and time its sum_as_string as a stand-in, so you can see the workflow before you swap in your scoring function:
mkdir string_sum
cd string_sum
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip maturin
maturin init # choose pyo3 when prompted
maturin develop --release
python -m timeit -s "import string_sum" "string_sum.sum_as_string(5, 20)"Now replace it with your scoring function and run the same timeit on your own data, against the Python original. Then apply the rule: if the extension meets your latency target, stop. Only when the hot path is most of the service does a full rewrite earn its cost.
What this means for your decision
Stay in Python unless a profiler has named a hot path. If it has, learn Rust as a second language through one small extension, not a rewrite. That gives a few people real Rust experience, keeps the product in the language the rest of the team ships in, and gives you a measured answer before you commit more.
Rewrite whole components only when the component is the hot path, or when you're writing something new that has no Python to integrate with. For that case, Go or Rust for a backend service is the next read. If other stacks are on your shortlist too, our other stack comparisons cover them one by one.
FAQ
Is Rust faster than Python?
For CPU-bound code, usually yes, since Rust compiles to native code. For I/O-bound services the gap often disappears, in our judgment. Time your own function in Python and in a release-built extension.
Can Rust replace Python?
Not for most teams. In Stack Overflow's 2025 survey, 54.8% of professional developers had done extensive work in Python against 14.5% for Rust, and tools like Ruff and Pydantic show the common pattern: a Python interface over a Rust core.
Is Rust better for AI than Python?
Not as a replacement. ML frameworks and notebooks are Python, and rewriting them rarely pays. Rust fits underneath, in a performance-critical component. For the framework choice, read PyTorch vs TensorFlow for ML teams.
Should a Python developer learn Rust?
If you've profiled a hot path, yes, and a PyO3 extension is a good first project. If you haven't, there's no pressing reason. Your Python skills cover far more of the work you'll meet.
Can you use Rust inside a Python project?
Yes. PyO3 provides the Rust bindings for Python, and maturin builds the extension and a distributable wheel. You import the result like any Python module.
Is Rust harder than Python?
Yes, in our experience. Ownership and the borrow checker are new to most Python developers, and the compiler is strict. Expect a slower first month.
