我正在学习如何在Python中嵌入Rust函数,并且如果我的输入是int
s,但没有列表,则一切正常。将Python列表传递给嵌入的Rust函数
如果我lib.rs
文件是:
#[no_mangle]
pub extern fn my_func(x: i32, y: i32) -> i32 {
return x + y;
}
我可以用这个如下:如果我改变
In [1]: from ctypes import cdll
In [2]: lib = cdll.LoadLibrary("/home/user/RustStuff/embed/target/release/libembed.so")
In [3]: lib.my_func(5,6)
Out[3]: 11
但是我lib.rs
到:
#[no_mangle]
pub extern fn my_func(my_vec: Vec<i32>) -> i32 {
let mut my_sum = 0;
for i in my_vec {
my_sum += i;
}
return my_sum;
}
我可以不再在Python中使用它(编译的很好):
In [1]: from ctypes import cdll
In [2]: lib = cdll.LoadLibrary("/home/user/RustStuff/embed/target/release/libembed.so")
In [3]: lib.my_func([2,3,4])
---------------------------------------------------------------------------
ArgumentError Traceback (most recent call last)
<ipython-input-3-454ffc5ba9dd> in <module>()
----> 1 lib.my_func([2,3,4])
ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
的原因,不过,我觉得这可能是工作是Python的list
和锈病的Vec
是两个动态数组,但显然我在这里失去了一些东西......
为什么我尝试不工作?我应该怎么做才能解决它?