我有一个接受 Python 列表的函数。我希望列表由字符串组成。我如何提取它们?
fn f(list: &PyList) -> PyResult<SomeClass> {
for obj in list.iter() {
let rust_string = PyString::from_object(obj, "ASCII", "strict")?.to_string()?;
// fails with `TypeError: decoding str is not supported`
if PyString::is_exact_instance(obj) {
let py_str: PyString = unsafe {std::mem::transmute(str)};
let rust_str = py_str.to_string()?;
// panics with failed assertion in PyString.as_bytes()
}
}
// ...
}
// Python call site
f(["string1", "string2"])
软件版本:Python 3.7、nightly Rust 1.33.0、pyo3 0.5.2。