Earlier in this chapter, we used a struct as a parameter. In C, there is nothing to stop the developer passing a structure as a parameter:
struct MyStruct; struct MyOtherStruct; void pass_struct(struct MyStruct *arg); void pass_struct2(struct MyOtherStruct *arg2);
MyStruct and MyOtherStruct are known as opaque structs. The name is exposed, but the private parts aren't.
Handling a struct within Rust is not as simple as you'd first think, but then it's not that difficult either. The only difference is that we use an empty enum instead of a struct when interfacing with the C library. This creates our opaque type that stores the information from the C opaque type. As the enum is empty, we can't instantiate it and, more importantly, as MyStruct and MyOtherStruct aren't the same, we have type-safety and so can't get them mixed up:
enum MyStruct {}; enum MyOtherStruct {}; extern "C" { pub fn pass_struct(arg: *mut MyStruct); pub fn pass_struct2(arg: *mut MyOtherStruct); }