Conceptual
Login

Controlling Struct Layout with repr C

Rust normally arranges the fields of a struct however it likes, which is fine until something outside your program expects a particular byte order. Asking for C layout pins the fields down in the order you wrote them, with the same padding a C compiler would use. You need that whenever a struct is shared with C code or describes a block of bytes a device reads.

Questions this Concept answers

  • Why is the default Rust layout unsuitable for a struct shared with C?