Let’s build and optimize a Rust extension for Python

If your Python code isn’t fast enough, you have many options for compiled languages to write a faster extension.
In this article we’ll focus on Rust, which benefits from:

  • Modern tooling, including a package repository called crates.io, and built-in build tool (cargo).
  • Excellent Python integration and tooling.
    The Rust package (they’re known as “crates”) for Python support is PyO3.
    For packaging you can use setuptools-rust, for integration with existing setuptools projects, or for standalone extensions you can use Maturin.
  • Memory- and thread-safe, so it’s much less prone to crashes or memory corruption compared to C and C++.

In particular, we’ll: