Talk "Accelerate Python with Rust".
Example code: https://github.com/astrojuanlu/rode
To see the slides:
uvx marimo run slides.py
Flat structure:
$ uvx maturin new -b pyo3 --src guessing-game
$ cd guessing-game
$ # Adjust Python version
$ sd 'requires-python = ">=3.8"' 'requires-python = ">=3.9"' pyproject.toml
$ # Add features to pyo3 Rust dependency
$ sd 'pyo3 = "(.*)"' 'pyo3 = { version = "$1", features = ["abi3-py39"] }' Cargo.toml
$ # Just launch Python (runs 'maturin' under the hood!)
$ uv run python
>>> import guessing_game
>>> guessing_game.sum_as_string(2, 3)
'5'
>>> ^D
Now remember to add this to pyproject.toml
:
[tool.uv]
# Rebuild package when any rust files change
cache-keys = [{file = "pyproject.toml"}, {file = "rust/Cargo.toml"}, {file = "**/*.rs"}]
# Uncomment to build rust code in development mode
# config-settings = { build-args = '--profile=dev' }
(From PyO3/maturin#2314 (comment))
Alternative, with workspaces (inspired by astral-sh/uv#9015 (comment)):
$ uv init
$ mkdir packages
$ uvx maturin new -b pyo3 --src packages/guessing-game
$ pushd packages/guessing-game
$ # Adjust pyproject.toml, same as before (including `cache-keys`)
$ sd ...
$ popd
Then add this to pyproject.toml
:
dependencies = [
"guessing-game",
]
[tool.uv.workspace]
members = ["packages/*"]
[tool.uv.sources]
guessing-game = { workspace = true }
You will need to configure rust-analyzer.linkedProjects
manually
because the extension only auto-discovers one level deep:
$ cat .vscode/settings.json
{
"rust-analyzer.linkedProjects": [
"packages/guessing-game/Cargo.toml"
]
}
More:
- What about
maturin generate-ci github
?