
repr(C) keeps kind first and aligns length to its own boundary, with padding between them.Understand it
Rust's default layout may change for optimization and is not an FFI contract. repr(C) gives C-compatible field ordering while fixed integer types make sizes explicit.
Rust's default struct representation is chosen for Rust code, not as an external wire or ABI contract. #[repr(C)] requests C-compatible field ordering and layout rules for the target, including padding between fields. It does not make a struct packed, remove padding, or guarantee the same total size on every architecture. Fixed-width fields such as u16 and u32 make individual sizes explicit, but alignment and endianness still matter when bytes cross a file or network boundary.
For the example Header, draw offsets for kind and length under a typical C layout and locate any padding before the u32. Then compare the result with a C declaration built for the same target. Use size_of and align_of as checks, not as the only specification. Do not serialize a repr(C) struct by copying its raw bytes into a protocol: padding can be uninitialized, and a different machine may use another byte order. Define a wire format with explicit field widths and endianness instead. Reserve repr(C) for genuine language or ABI interoperability where both sides agree on ownership and representation.
For unions, bitfields, and packed data, the boundary becomes more complicated than a simple struct. A packed field may be unaligned, so taking an ordinary reference to it can be invalid. Read or write such representations through the appropriate unaligned operations when required. Most application data should use a deliberate parser rather than treating external bytes as an in-memory Rust struct.
Read the code
#[repr(C)]
struct Header {
kind: u16,
length: u32,
}repr(C) keeps kind first and aligns length to its own boundary, with padding between them.
Practice starter
use std::mem::{align_of, offset_of, size_of};
#[repr(C)]
struct Header {
kind: u16,
length: u32,
}
fn main() {
println!("{}", offset_of!(Header, length));
println!("{} {}", size_of::<Header>(), align_of::<Header>());
}Try it
Predict both printed lines before running this. Then remove #[repr(C)] and say which of those numbers you may still rely on.
What success looks like: You can locate the two padding bytes and say why the default layout is not an FFI contract.
Check your understanding
Why does an FFI struct need repr(C)?
Reveal the answer
- To define its layout
- To allocate it on the heap
- To make it Copy
repr(C) fixes field order and padding, so on a typical target length sits at offset 4, after two padding bytes.
Go deeper
Common misconception: “Rust stores struct fields in the order you write them, so #[repr(C)] changes nothing.”
Rust's default layout may change for optimization and is not an FFI contract. Write #[repr(C)] when field order and padding must match the other side.
- Place kind:
repr(C)keeps declaration order, so theu16namedkindsits at offset 0. - Align length: A
u32usually aligns to 4 bytes, so two padding bytes followkindandlengthstarts at offset 4. - Check the total: The struct takes its largest field alignment, giving size 8 and alignment 4 on typical targets. Confirm with
size_ofandalign_of.
Transfer challenge: Swap the two fields, predict the offsets and size_of, then try #[repr(C, packed)] on the original order. Say why taking &header.length from the packed form is rejected.
Connections
- Raw Pointers: Represent an address without borrow guarantees
- Unsafe Traits: Make an implementation promise other unsafe code relies on
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Representation & Layout (courseId "systems", lessonId "repr-layout") Link: https://crabrix.com/learn/systems/repr-layout/ Goal: Rust's default layout may change for optimization and is not an FFI contract. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=systems/repr-layout) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.