Pass numpy array to rust function using pyo3

Hi all.

I am using rust to make some optimizations to my python code. In the process I am learning how to call rust functions form python. I am using pyo3. I need rust to work on some arrays (numpy). I was able to understand simple cffi calls, but couldn't get any example out there explaining array slices cffi in pyo3.

Here is a working folder of me practicing rust ffi. Here I am trying to implement using vectors from python, which doesn't compile.

error[E0277]: the size for values of type `[f32]` cannot be known at compilation time
  --> src/lib.rs:26:1
   |
26 | #[pyfunction]
   | ^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `[f32]`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because of the requirements on the impl of `pyo3::conversion::FromPyObject<'_>` for `&[f32]`
   = note: required by `pyo3::objectprotocol::ObjectProtocol::extract`

error[E0277]: the trait bound `[f32]: pyo3::typeob::PyTypeInfo` is not satisfied
  --> src/lib.rs:26:1
   |
26 | #[pyfunction]
   | ^^^^^^^^^^^^^ the trait `pyo3::typeob::PyTypeInfo` is not implemented for `[f32]`
   |
   = note: required because of the requirements on the impl of `pyo3::conversion::PyTryFrom` for `[f32]`
   = note: required because of the requirements on the impl of `pyo3::conversion::FromPyObject<'_>` for `&[f32]`
   = note: required by `pyo3::objectprotocol::ObjectProtocol::extract`

error: aborting due to 2 previous errors

Here is an example of python using rust functions.

Hi @dineshadepu,

pyo3 currently doesn't have conversions to &[f32], which causes the error shown. Since you're using numpy, you should use rust-numpy, which also has the relevant conversions. (Note that numpy types are normal python types, so pyo3 generally can't know how to convert them).