I'm trying to call Rust code from my C project for an embedded device. The device prints over UART, so I am able to see what the result of my call is.
The following C and Rust code works as expected (I have omitted a lot of boilerplate Rust code that is needed to make it compile).
C:
uint8_t input[] = {1,2,3};
uint8_t output[] = {4,5,6};
output = func(input, output);
printf("Sum: %d", output[0]);
Rust:
#[no_mangle]
pub extern fn func(input: &[u8], dst: &mut[u8]) -> u8 {
3
}
This prints 3 as expected. But I'm stuck at mutating the arrays passed in as references:
C:
uint8_t input[] = {1,2,3};
uint8_t output[] = {4,5,6};
func(input, output);
printf("Sum: %d", output[0]);
Rust:
#[no_mangle]
pub extern fn func(input: &[u8], dst: &mut[u8]) {
for i in (0..1) {
dst[i] = input[i];
}
}
This compiles, but prints 4 instead of the expected 1. For some reason I'm not able to change the value of the array. Any ideas?
EDIT: The C function declarations are respectively:
extern uint8_t func(uint8_t in[64], uint8_t output[64]);
extern void func(uint8_t in[64], uint8_t output[64]);
EDIT2: Updated code: C:
uint8_t input[64];
uint8_t output[64];
for(uint8_t = 0; i < 64; i++) {
input[i] = i;
}
func(input, output);
printf("Sum: %d", output[2]);
Expects output 2.