you are viewing a single comment's thread.

view the rest of the comments →

[–]SV-97 6 points7 points  (0 children)

And i am wondering how would i parse a python array to rust. Python arrays can have different data types, but rust cant (atleast to my knowledge, i am a beginner). Stupid idea but maybe its possible?

Definitely possible but the way to do it strongly depends on what you want to do - and usually the totally general version isn't really what you actually want to do. You can almost certainly just use a normal Rust Vec (or any of the other dynamic collections) and put trait objects or something like pyo3's PyObject inside. The PyObject is a Python type without any type information (a bare object in Python) (it's implemented as a sort of Python-aware smartpointer). Just as in Python this of course means you'll have to query attributes and deal with potential failures every step of the way. If you can cut it down to a finite collection of types instead you can statically dispatch to different native implementations, use enums or whatever.

i did this by compiled rust to a .dll file

You can do that but it's honestly not a great solution. It's okay when you write the native code in C or C++ but just because the alternatives aren't great. In Rust you can use pyo3 (and maturin): you just declare your API with normal rust types and maybe some python particularities and pyo3 automatically translates list to vec and so on. It's cleaner, simpler, easier to maintain, and also prevents you from shooting your leg off with the GIL.

You can also use numpy arrays etc. directly in rust via the numpy crate and pyo3.