IT lexicon › Programming › repr(C)

repr(C)

Programming På svenska → Updated: 2026-11-06

The Rust attribute that tells the compiler to lay out a struct exactly as a C compiler would.

Rust's default layout promises nothing. Since version 1.18 in 2017 the compiler reorders fields to minimise padding, and the order may change between releases. Great for memory, disastrous for anything sharing data with C. #[repr(C)] pins the field order and applies the platform's C rules for size and alignment.

The family is larger: repr(transparent) guarantees a wrapper has the same ABI as its single field, repr(u8) fixes an enum's discriminant type, and repr(packed) throws away padding at the cost of unaligned loads. Note that the attribute covers layout only. The calling convention is set separately with extern "C" — forgetting one of the pair is a classic FFI bug.

← Back to the lexicon