相关疑难解决方法(0)

使用数组参数从C调用Rust方法

我正在尝试从我的C项目中为嵌入式设备调用Rust代码.设备通过UART打印,因此我可以看到我的通话结果是什么.

下面的C和Rust代码按预期工作(我省略了许多使其编译所需的样板代码).

C:

uint8_t input[] = {1,2,3};
uint8_t output[] = {4,5,6};
output = func(input, output);
printf("Sum: %d", output[0]);
Run Code Online (Sandbox Code Playgroud)

锈:

#[no_mangle]
pub extern fn func(input: &[u8], dst: &mut[u8]) -> u8 {
  3
}
Run Code Online (Sandbox Code Playgroud)

这按预期打印3.但我坚持改变作为参考传入的数组:

C:

uint8_t input[] = {1,2,3};
uint8_t output[] = {4,5,6};
func(input, output);
printf("Sum: %d", output[0]);
Run Code Online (Sandbox Code Playgroud)

锈:

#[no_mangle]
pub extern fn func(input: &[u8], dst: &mut[u8]) {
  for i in (0..1) {
      dst[i] = input[i];
  }
}
Run Code Online (Sandbox Code Playgroud)

这编译,但打印4而不是预期1.由于某种原因,我无法更改数组的值.有任何想法吗?

编辑:C函数声明分别是:

extern uint8_t func(uint8_t in[64], uint8_t output[64]);
extern void func(uint8_t in[64], uint8_t …
Run Code Online (Sandbox Code Playgroud)

c ffi rust

4
推荐指数
1
解决办法
1190
查看次数

标签 统计

c ×1

ffi ×1

rust ×1