【发布时间】:2015-06-26 13:51:14
【问题描述】:
我已经学习 Rust 大约两个星期了,今天,我进入了它的 FFI。我使用 Python 来玩 Rust,使用 ctypes 和 libc。我传递了整数、字符串,甚至学会了传递整数列表 (thanks to this wonderful answer)。
然后,我尝试传递一个字符串列表(按照该答案背后的推理),但我失败了,因为我无法获得领先。在 Python 中,我有这样的东西来传递字符串数组。
def testRust():
lib = ctypes.cdll.LoadLibrary(rustLib)
list_to_send = ['blah', 'blah', 'blah', 'blah']
c_array = (ctypes.c_char_p * len(list_to_send))()
lib.get_strings(c_array, len(list_to_send))
在 Rust 中,我认为应该有一些东西(比如 STRING_RECEIVER)来收集传入的字符串,但我找不到。
#![feature(libc)]
extern crate libc;
use std::slice;
use libc::{size_t, STRING_RECEIVER};
#[no_mangle]
pub extern fn get_strings(array: *const STRING_RECEIVER, length: size_t) {
let values = unsafe { slice::from_raw_parts(array, length as usize) };
println!("{:?}", values);
}
有没有其他方法可以做到这一点?
【问题讨论】: