use pyo3::prelude::*;
use std::collections::HashMap;
#[derive(FromPyObject, Debug)]
pub enum Prop {
Int(usize),
String(String),
Vec(Vec<usize>),
}
#[pyfunction]
pub fn get_props(props: HashMap<String, Prop>) -> PyResult<()> {
let v = props.into_iter().collect::<Vec<(String, Prop)>>();
for i in v {
println!("K = {}, V = {:?}", i.0, i.1)
}
Ok(())
}
来自python:
import pyo3_example
pyo3_example.get_props({
"name": "Shivam Kapoor",
"age": 35,
"hobbies": [1, 2, 3]
})
# K = name, V = String("Shivam Kapoor")
# K = age, V = Int(35)
# K = hobbies, V = Vec([1, 2, 3])
1条答案
按热度按时间iqih9akk1#
从github复制。
这种方法的问题是它不是很Python化。只有当用户可以提供一个Prop列表作为字典时,它才是理想的。我已经成功地实现了它,如下所示:
来自python: