PyData Seattle 2023

Nine Rules for Writing Python Extensions in Rust
04-28, 14:15–15:00 (America/Los_Angeles), Hood

Python extensions let you speed up your code by calling fast code written in other languages. Traditionally, you would write your extensions in C/C++. Rust offers an alternative to C/C++ with these benefits:

  • As fast as C/C++
  • Much better memory safety and security than C/C++
  • Most loved programming language since 2016
  • Multithreading without needing a runtime

In this talk, we’ll cover nine rules that I learned as I ported our open-source genomics extension from C++ to Rust. This will help you get started and help you organize your project.

If you’re a seasoned extension writer frustrated with C/C++, or a beginner looking to write your first extension, this talk is for you!


The outline of the talk goes as follows:

First, an introduction to Python extensions and the Rust programming language.

Then, the rules:

  • Create a single repository containing both Rust and Python projects
  • Use maturin & PyO3 to create Python-callable translator functions in Rust
  • Have the Rust translator functions call “nice” Rust functions
  • Preallocate memory in Python
  • Translate nice Rust error handling into nice Python error handling
  • Multithread with Rayon and ndarray::parallel, returning any errors
  • Allow users to control the number of parallel threads
  • Translate nice dynamically-type Python functions into nice Rust generic functions
  • Create both Rust and Python tests

After the talk, you'll have the tools for writing performant, secure, and loved extensions for Python.


Prior Knowledge Expected

No previous knowledge expected

Ph.D. in CS and Machine Learning. Retired Microsoft & Microsoft Research. Volunteer, open-source projects related to ML, Genomics, Python, and Rust.

This speaker also appears in: