Rust has enjoyed fantastic synergy with Python; it’s one of the reasons Rust has been doing so well in the Python extension module landscape. There are two key projects: PyO3, which functions as a powerful pure Rust library for creating extensions (very much like pybind11 for C++), and Maturin, a very simple modern build system tied to Cargo (much like Scikit-build-core and CMake for C/C++/Fortran languages).
Getting started¶
Creating a new project is easy. Install maturin (cargo install maturin,
brew install maturin, pipx install maturin, etc). Then you can use the
maturin command line tool to quickly make a new project:
$ maturin new rust_exampleIf you don’t provide a binding mechanism to use, it will ask you. It will
default to a pure-Rust project; you can add flags to get a mixed Python and Rust
project instead (most more advanced projects will have at least some Python
parts). Check the flags with --help/-h.
Now, you should have a project like this:
rust_example
├── Cargo.toml
├── pyproject.toml
└── src
└── lib.rsYour Cargo.toml should look something like this:
[package]
name = "rust_example"
version = "0.1.0"
edition = "2024"
[lib]
name = "rust_example"
crate-type = ["cdylib"]
[dependencies]
pyo3 = {version = "0.30.1", features = ["abi3-py311"]}The standard rust package stuff is at the top. The lib table has the library
name (this must match the module name), and the crate-type, which must include
cdylib to be importable in Python. If you want to access it from Rust too, you
can add to this list.
The line that might look a little different vs. the template is the
dependencies; we use a recent version of pyo3, and we are compiling for the
Limited ABI. This lets us compile without needing Python (at least on Unix) and
lets us support all versions of Python newer than some minimum with a single
binary. The cost is a
few features will be disabled (less if the version is higher), and some
optimizations will be disabled.
The pyproject.toml file should look pretty normal:
[build-system]
requires = ["maturin>=1.16,<2.0"]
build-backend = "maturin"
[project]
name = "rust_example"
requires-python = ">=3.11"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]This is pretty similar to other build backends. Maturin detects PyO3 from
Cargo.toml, so no tool.maturin section is needed. It is able to pull the
version from the Cargo.toml file if you include "version" in the dynamic
list.
Finally, we have the library itself. Here’s the simple template example,
src/lib.rs:
use pyo3::prelude::*;
/// A Python module implemented in Rust.
#[pymodule]
mod rust_example {
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
}It starts with a use statement that pulls in the basics for using PyO3.
The module is a Rust inline module annotated with #[pymodule]. Everything
annotated inside it is added to the Python module.
Then there’s a docstring on a function (notice the triple slashes) - PyO3 will actually be able to capture this for the Python docstring! You can’t natively do this in C++; you can write scripts that try to collect these, but Rust supports it natively.
The function is annotated with #[pyfunction], which will make it a function
you can add to Python. You return a PyResult<...> if a function could “throw”
an error in Python. Functions that don’t ever throw errors can just return
normal Rust values that have known conversions. Otherwise, it’s pretty normal.
(Actually, we don’t ever return a non-OK value in this example, so feel free to
simplify this to just return String).
This declarative interface has been the default since PyO3 0.22. In older code,
you will see the classic interface. There, the module is a function that takes a
Bound PyModule and runs .add_* functions on it (much like pybind11). Functions
need to be given in the wrap_pyfunction! macro:
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
/// A Python module implemented in Rust.
#[pymodule]
fn rust_example(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}Our square example would look like this:
use pyo3::prelude::*;
#[pymodule]
mod rust_example {
use pyo3::prelude::*;
#[pyfunction]
fn square(a: usize) -> usize {
a * a
}
}Building and running¶
You can use all the usual Python tools (like pip, build, etc), but you can also
build directly with maturin. In many cases, this will skip many of the Python
calls altogether. The maturin build command will build a wheel. The
maturin develop command will do an editable install (requires pip in the
venv you are building in, or pass --uv to use uv instead).