I - Install pip package
pip install maturin
II - Create project
maturin new --bindings pyo3 --mixed name_project
III - Create rust lib
Create in src
matrix.rs
with your code like this
use rand::Rng;
pub fn generate_matrix() -> Vec<Vec<i32>> {
let mut matrix = vec![];
for _ in 0..10 {
let mut row = vec![];
for _ in 0..10 {
row.push(rand::thread_rng().gen_range(0..100));
}
matrix.push(row);
}
matrix
}
In src/lib.rs
add your code like this :
mod matrix;
#[pyfunction]
fn random_matrix() -> Vec<Vec<i32>> {
matrix::generate_matrix()
}
And functions in the python module like this :
#[pymodule]
fn my_project(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(random_matrix, m)?)?;
Ok(())
}
IV - Compile
maturin build && pip install .
V - Test
In python/name_project/test/
for exemple, create one file with this and run it.
import my_project
matrix = my_project.random_matrix()
print(matrix)
Top comments (0)