Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Attributes

The #[repr] attribute

Reference for Rust #[repr]: repr(Rust), C, u8 and other integer types, transparent, packed and align, with size and alignment results you can run.

By default the compiler is free to lay out a struct or enum however it likes: it may reorder fields to save padding, and it makes no promises across compiler versions. #[repr(...)] overrides that choice when you need a layout that is predictable, for example to talk to C code, to fix an enum's integer size, or to control alignment.

The main representations are C (the layout a C compiler would use), an integer type such as u8 or i32 for enums, transparent (same layout as the single non-zero-sized field), packed (no padding) and align(N) (minimum alignment).

Rust
#[repr(C)]
struct Header { tag: u8, len: u32 }

Try it

Rust Sizes under different representations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Fixed enum representation and transparent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
#[repr(C)]
#[repr(u8)] / #[repr(i32)] / ...
#[repr(transparent)]
#[repr(packed)] / #[repr(packed(N))]
#[repr(align(N))]
#[repr(C, u8)]          // combinations on enums
Form Effect
Rust (default) Unspecified layout; fields may be reordered
C Fields in declaration order with C padding rules; stable and FFI-safe for compatible field types
u8, i32, ... Enum tag has that integer type; fieldless enums become C-like integers
transparent Struct or enum with exactly one non-zero-sized field has the same layout and ABI as that field
packed / packed(N) Removes padding (or limits alignment to N bytes)
align(N) Raises the type's alignment to N, a power of two

Errors and edge cases:

  • packed and align cannot be combined on the same type; packed fields cannot be borrowed by reference when it would be misaligned (hard error for references, because that is undefined behaviour).
  • repr(C) on an enum with fields gives a defined tagged-union layout; repr(u8) on an enum with fields defines a union of repr(C) structs, each starting with the tag.
  • Casting an integer to a repr(u8) enum is not allowed with as; use a match or TryFrom since not every integer is a valid variant (transmuting an invalid value is undefined behaviour).

Usage notes

  • Do not reach for repr(C) without a reason. The default layout is usually smaller. Use C for FFI, file formats and shared-memory structs.
  • Null-pointer optimization is a default-layout feature: Option<&T> and Option<Box<T>> are pointer-sized. #[repr(transparent)] wrappers keep it.
  • Check sizes in tests. size_of and align_of make a cheap regression test for layouts that other code depends on.
  • packed costs speed on some CPUs, because fields can be misaligned.
  • Newtypes for FFI. #[repr(transparent)] is what lets a wrapper type be passed where its inner type is expected across an FFI boundary.
  • Enums and as. Fieldless enums can be cast to integers with as regardless of repr, but only a primitive repr fixes the size.