To initialize a Rust project, use:
cargo new <project-name>In this case, it is recommended to use maturin:
maturin new -b pyo3 <project-name>This will generate the necessary configurations.
To keep the Python project structure untouched, extract all components outside of the created folder into your existing project. Specifically:
- Move
srcto the same level asapp. - Place
Cargo.tomlandpyproject.tomlat the root of your project. - Delete the now-empty
<project-name>folder.
Once the Rust code is finished, build the Rust module by executing the following command from the root of the project:
maturin developThis command:
- Builds the Rust module.
- Installs it as a Python package in your virtual environment.
- Makes it available for import inside your app modules.