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.
- Learning objectives
- Workshop structure
- Setup
- Exercices
- Correction
- References and materials
- Acknowledgements
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.
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.
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.
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.
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)
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 syncFor a zero-installation experience directly in your browser:
-
Go to the repository on GitHub
-
Click the green "Code" button → "Create codespace"
-
Once the Codespace loads, run:
uv sync source .venv/bin/activateYou'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.
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.ipynbThe correction for the exercises is available in the src/poly_match_rs/src/correction folder.
