I am doing a python project that needs a bit of a speed boost, so I am making rust do a bit of the heavy lifting. What my project requires is that I can take in a numpy array, convert it to a rust array, apply some function to it, and return it as a numpy array. My only problem right now is specifying the lifetime of the return type (it is giving me the error “missing lifetime specifier
this function’s return type contains a borrowed value, but the signature does not say whether it is borrowed from py
or array
” under this bit of code:
-> PyResult<Vec<&str>>
).
All I really need right now is for that error to be fixed. Here is my full rust code:
use pyo3::wrap_pyfunction;
use numpy::PyArrayDyn;
#[pymodule]
fn dnd_images(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_shape_instructions, m)?)?;
Ok(())
}
#[pyfunction]
fn get_shape_instructions(py: Python, array: &PyArrayDyn<u16>) -> PyResult<Vec<&str>> {
let array = unsafe { array.as_array() };
let mut result: Vec<&str>;
for element in array{
let s: String = element.to_string();
result.push(&s);
}
Ok(result)
}
All I have tried, as I am not very experienced with rust, is mashing things together in different orders and functions with different types according forums, articles, tutorials, and GPT-4.
Reuben Badcock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.