Skip to content

datacraft-paris/2025-workshop-rust_for_python

Repository files navigation

Logo


Rust for Python - 2025/09/23

Rust is increasingly being adopted in areas that demand high performance and reliability, making it a valuable complement to Python in Data Science workflows.

With PyO3, you can seamlessly integrate Rust functions into your Python projects, combining Python’s flexibility with Rust’s speed and safety.

Table of Contents

  1. Learning objectives
  2. Workshop structure
  3. Setup
  4. Exercices
  5. Correction
  6. References and materials
  7. Acknowledgements

Learning objectives

After completing this workshop, you should be able to:

  • Integrate Rust within your Python environment using Py03, Cargo, and Maturin.
  • Write basic Rust code.
  • Compare different implementations' performances.

Workshop structure

This workshop is divided into three parts :

  • Introduction to Rust (~60 minutes)
  • Exercise : optimizing a Python script with Rust (~80 minutes)
  • Extra : perf comparison between different implementations on two example ML algorithms.

Introduction to Rust, Cargo and PyO3

This section covers everything you need to know about Rust to complete this workshop. We recommend using these as support materials and references for the upcoming exercises.

Exercise : Optimizing Python code with Rust

You will put your new knowledge into practice by optimizing a simple Python function that matches 2D points to polygons.

Starting from a baseline implementation, the goal is to progressively move parts of the computation to Rust to significantly speed up the process and evaluate performance gains.

Extra : Comparison of Rust vs Python implementations on ML algorithms

Comparing the performance of different machine learning algorithm implementations.

You will measure and visualize execution times for:

  • Python (naive implementation)
  • Python scikit-learn
  • Rust scratch (exposed to Python via PyO3)

Setup

Local setup

NB : You will need admin permissions to create flamegraphs with py-spy.

git clone git@github.com:datacraft-paris/2509-workshop-rust_for_python.git;
cd 2509-workshop-rust_for_python
  • Install Rust and Cargo
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
  • Install Uv
curl -LsSf https://astral.sh/uv/install.sh | sh
  • Create, activate your virtual environment and install dependencies
uv venv # at the root of the repo
source .venv/bin/activate && uv sync

Option 2 — Cloud Setup (GitHub Codespaces)

For a zero-installation experience directly in your browser:

  1. Go to the repository on GitHub

  2. Click the green "Code" button → "Create codespace"

  3. Once the Codespace loads, run:

    uv sync
    source .venv/bin/activate

    You're now ready to start coding!

Important: Each Codespace is tied to the branch you selected when creating it. To switch branches, go back to GitHub, select the new branch, and click on the + icon in the top right corner to create a new Codespace for that branch then repeat step 4.

illustration

Exercises

Select your desired difficulty by switching to the corresponding branch :

git checkout easy # EASY level : mostly commented code, easy follow-through.
git checkout main # DEFAULT = INTERMEDIATE
git checkout hard # HARD level : no guidance 

Open the workshop's notebook by launching this command in your virtualenv :

jupyter-notebook ./202509-workshop-00-rust_for_python.ipynb
  • Bonus part

If you want more examples of Rust in Python implementations, take a look at this second notebook where we will compare performances for Rust vs Python for k-means and k-fold stratification.

jupyter-notebook ./202509-workshop-01-rust_for_python-EXTRAS.ipynb

Correction

The correction for the exercises is available in the src/poly_match_rs/src/correction folder.

References and materials

Acknowledgements

About

Workshop - Rust for Python with Py03

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •