I'm using Rust to test some C code:
lol.c
#include "lol.h"
int a[10]; //Assume lol.h has an extern declaration for a[10]
lib.rs
extern "C" {
static a: *mut i32;
}
fn set_a(val: i32, index: usize) {
assert!(index < 10);
unsafe {
a[index] = val;
}
}
fn get_a(index: usize) {
assert!(index < 10);
unsafe { a[index] }
}
I used the cc crate to compile and link lol.o. How do I write the set_a
and get_a
functions? The compiler says:
error[E0608]: cannot index into a value of type `*mut i32`
--> src/main.rs:8:9
|
8 | a[index] = val;
| ^^^^^^^^
error[E0608]: cannot index into a value of type `*mut i32`
--> src/main.rs:14:14
|
14 | unsafe { a[index] }
| ^^^^^^^^