Using NumPy and other Python modules in Rust
In this section, we are going to understand the basics of importing a Python module such as NumPy in our Rust program and return the result to our Python function. We will build our functionality in our Fibonacci number package that we have been coding so far in this book. We will also briefly explore importing a Python module in a generic sense so that you experience how to use a Python module that has the functionality you are relying on. We will build a more comprehensive approach to using Python modules in our Rust code in the next section. For this section, we will write all our code in the src/lib.rs
file. Here are the steps we need to take:
- First, we need to acknowledge that we pass in a dictionary and return the results in it. Because of this, we must import the
PyDict
struct by running the following code:use pyo3::types::PyDict;
- Now that this is imported, we can define our function by running the following code:
#[pyfunction...