Explanation and tutorial:
https://www.badprog.com/rust-python-binding-setting-up-pyo3-crate-with-maturin
To generate the Rust binding library we're going to use Maturin.
It's a tool used to manage the compilation and the path where the dynamic library will be generated.
Before going further, let's create a virtual environment for Python:
python -m venv .venv1Then, let's activate it:
. .venv1/bin/activateWith our Python setup ready, let's install the Maturin Python tool:
pip install maturinUse it to generate the dynamic library then allowing Python to access it:
maturin develop -m classic_operations/Cargo.tomlNote here that we need to explicitely specify where is the Cargo.toml file to load because we use subprojects (potentially many Cargo.toml files).
It's also possible to go to the subproject directly and type the command without specifying the path of Cargo.toml (just "maturin develop").
It's now time to check with our Python file if our library really works.
python binding.pyYou should see this output:
Total add: 213.0
Total sub: -167.0
Total div: 0.45
Total mul: 1320.0