Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Rust and python bindings
victor_dalet
victor_dalet

Posted on • Edited on

     

Rust and python bindings

I - Install pip package


pip install maturin
Enter fullscreen modeExit fullscreen mode

II - Create project


maturin new --bindings pyo3 --mixed name_project
Enter fullscreen modeExit fullscreen mode

Image description

III - Create rust lib


Create insrcmatrix.rs with your code like this

userand::Rng;pubfngenerate_matrix()->Vec<Vec<i32>>{letmutmatrix=vec![];for_in0..10{letmutrow=vec![];for_in0..10{row.push(rand::thread_rng().gen_range(0..100));}matrix.push(row);}matrix}
Enter fullscreen modeExit fullscreen mode

Insrc/lib.rs add your code like this :

modmatrix;#[pyfunction]fnrandom_matrix()->Vec<Vec<i32>>{matrix::generate_matrix()}
Enter fullscreen modeExit fullscreen mode

And functions in the python module like this :

#[pymodule]fnmy_project(m:&Bound<'_,PyModule>)->PyResult<()>{m.add_function(wrap_pyfunction!(random_matrix,m)?)?;Ok(())}
Enter fullscreen modeExit fullscreen mode

IV - Compile


maturin build && pip install .
Enter fullscreen modeExit fullscreen mode

V - Test


Inpython/name_project/test/ for exemple, create one file with this and run it.

importmy_projectmatrix=my_project.random_matrix()print(matrix)
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi....
  • Location
    Paris
  • Education
    ESGI
  • Work
    computer vision developer
  • Joined

More fromvictor_dalet

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp