how can i create a heterogeneous python script in rust with pyo3?

I’m not a user of pyo3, but from Rust’s point of view the problem is that you try to make a heterogeneous array, which is prohibited anyway. To solve it you may try to convert it into a homogeneous array by utilizing PyObject type of the crate. I can’t test if the next snippet works and I’m not sure if you can make it simpler, but the idea holds:

let key_vals: &[(&str, PyObject)] = [ ("num", 8.to_object()), ("str", "asd".to_object()) ]
let dict = key_vals.into_py_dict(py);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top