skip to content

Type Layout and Representation

Rust may reorder struct fields unless repr(C) says otherwise, and niches let Option<&T> stay pointer-sized. Interviewers ask about size, alignment and which layouts are guaranteed.

on this pageshow

questions

5

In Rust, why is `Option<&u64>` the same size as `&u64` while `Option<u32>` is twice the size of `u32`, and which sizes are guaranteed?

level: middleimportance: must knowfreq 55%

answer

  1. values a type can never hold
  2. null is impossible for a reference
  3. every u32 bit pattern is valid
  4. a documented list in core::option

basics

~20 s

A reference is never null, so rustc encodes None as the null pointer and Option<&u64> stays 8 bytes. Every u32 bit pattern is valid, so Option<u32> needs a separate tag. Only a documented list of types gets this guarantee.

solid answer

~40 s

A **niche** is a bit pattern a type is never allowed to hold. `&T`, `&mut T`, `Box<T>`, `NonNull<T>`, function pointers and `NonZero<u32>` (also spelled `NonZeroU32`) can never be zero, so `Option` stores `None` as all-zero bytes and adds nothing. `u32` has no invalid patterns, so `Option<u32>` adds a discriminant and padding: 8 bytes. The `core::option` docs guarantee that `Option<T>` has the same size, alignment and function-call ABI as `T` for exactly those types and for `#[repr(transparent)]` wrappers around them, which is why `Option<extern "C" fn()>` or `Option<&T>` can stand for a nullable C pointer. Other niches, such as `Option<bool>` being 1 byte, happen today but are not promised.

code

rust · 20 lines
rust
use std::num::NonZero;
use std::ptr::NonNull;

fn main() {
    println!("&u64            {}", size_of::<&u64>());
    println!("Option<&u64>    {}", size_of::<Option<&u64>>());
    println!("Option<Box<u64>> {}", size_of::<Option<Box<u64>>>());
    println!("Option<NonNull<u8>> {}", size_of::<Option<NonNull<u8>>>());
    println!("Option<fn()>    {}", size_of::<Option<fn()>>());
    println!("u32             {}", size_of::<u32>());
    println!("Option<u32>     {}", size_of::<Option<u32>>());
    println!("Option<NonZero<u32>> {}", size_of::<Option<NonZero<u32>>>());
    println!("Option<bool>    {}", size_of::<Option<bool>>());
    println!("Option<char>    {}", size_of::<Option<char>>());
    println!("Option<&[u8]>   {}", size_of::<Option<&[u8]>>());
    println!("Option<Option<bool>> {}", size_of::<Option<Option<bool>>>());
    println!("*const u8 {} Option<*const u8> {}", size_of::<*const u8>(), size_of::<Option<*const u8>>());
    println!("Vec {} Option<Vec> {}", size_of::<Vec<u8>>(), size_of::<Option<Vec<u8>>>());
    println!("Result<&u8,()> {}", size_of::<Result<&u8, ()>>());
}

go deeper

for a junior

Recall that a reference is never null, so Option<&T> costs nothing extra, while Option<u32> needs a tag.

for a middle

Explain niches as forbidden bit patterns, list the guaranteed types from core::option, and show why raw pointers get no niche.

for a senior

Separate guarantee from optimisation: rely on the listed types at FFI boundaries and in transmutes, and keep Option<bool> or Option<Vec<T>> sizes out of any contract.

for a principal

Use NonZero ids and Option<Box<T>> links to keep hot tables small, and pin only the guaranteed sizes with compile-time assertions.

## What a niche is Some Rust types have bit patterns they are never allowed to contain. A `&u64` is always non-null and aligned; a `bool` is only ever `0` or `1`; a `char` is never above `0x10FFFF`. Producing one of the forbidden patterns is undefined behaviour, so the compiler knows those patterns are free. Such a free pattern is called a **niche**. An enum like `Option<T>` normally needs a **discriminant**, a tag saying which variant is live, stored next to the payload. When the payload type has a niche, rustc can encode the dataless variant in that niche instead. For `Option<&u64>`, `None` is stored as the null pointer, `Some(r)` as the pointer itself, and no tag byte exists. ## Measuring the difference | Type | Size on a 64-bit target (rustc 1.99.1) | Why | |---|---|---| | `&u64`, `Option<&u64>` | 8, 8 | null is the niche | | `Box<u64>`, `Option<Box<u64>>` | 8, 8 | a `Box` is never null | | `Option<NonNull<u8>>`, `Option<fn()>` | 8, 8 | both are never null | | `u32`, `Option<u32>` | 4, 8 | no niche, so a tag plus padding | | `Option<NonZero<u32>>` | 4 | zero is the niche | | `*const u8`, `Option<*const u8>` | 8, 16 | raw pointers may be null, so no niche | | `Option<bool>` | 1 | rustc uses a value a `bool` never holds, but this is not guaranteed | The raw-pointer row is the useful contrast: `*const u8` may legally be null, so there is nothing free to borrow and `Option` has to add a tag. ## Which sizes are a guarantee The standard library documents the guarantee in the `core::option` module under "Representation". For these `T`, `Option<T>` has the **same size, alignment and function-call ABI** as `T`, and transmuting between `T` and `Option<T>` is sound: - `Box<U>` (with the global allocator), `&U` and `&mut U`; - `fn` pointers and `extern "C" fn` pointers, including `unsafe` and other ABIs; - the `NonZero` integer types, such as `NonZero<u32>`; - `ptr::NonNull<U>`; - a `#[repr(transparent)]` struct around one of the types above. For most of these the docs further guarantee that `None` is the all-zero bit pattern (for `&U`, `&mut U`, `Box<U>` and `NonNull<U>` this part requires `U: Sized`). The `core::result` docs extend the same promise to `Result<T, E>` when one side qualifies and the other is a zero-sized type with alignment 1, such as `Result<&T, ()>`. Everything outside that list is an **optimisation, not a contract**: - `Option<bool>` and `Option<char>` keep their payload's size on rustc 1.99.1, but nothing in std promises it. - `Option<Vec<u8>>` is 24 bytes like `Vec<u8>` because a vector's internals include values it never holds, such as a null pointer, again not a documented guarantee. - Nested options such as `Option<Option<bool>>` currently fit in 1 byte; do not build on it. ## Why the guarantee matters The guaranteed cases are what make `Option` usable at boundaries that care about bytes: 1. **Nullable pointers across an FFI boundary.** A C function taking a callback that may be `NULL` can be declared with `Option<extern "C" fn(i32)>`; `None` is passed as a null pointer. A C function returning a pointer that may be null can be declared as returning `Option<&T>` or `Option<NonNull<T>>`. 2. **Space in data structures.** A tree whose child links are `Option<Box<Node>>` pays one pointer per link, not a pointer plus a tag. 3. **Sentinel-free ids.** An id type built on `NonZero<u32>` makes `Option<Id>` cost nothing extra, which keeps large tables small. How enums choose a tag and size their variants in general is covered in the lesson on enums with payloads; the point here is the boundary between what rustc does and what it promises. ## Checking it in code You can pin the guarantees you rely on with compile-time assertions: `const _: () = assert!(size_of::<Option<&u64>>() == size_of::<&u64>());` fails the build if the layout ever disagrees. Assert only listed types this way, because an assertion on an unlisted case such as `Option<bool>` freezes an unpromised detail into your build. Two boundaries are worth stating in an interview. First, the guarantee is about `Option` and the listed `Result` shapes, not about every enum you write: your own two-variant enum around a reference will usually get the same layout, but nothing documents it. Second, the guarantee covers size, alignment and ABI, so it is what an `extern "C"` signature or a transmute may lean on; the optimisation cases are fine to *use* but give no basis for reinterpreting bytes.

  • In Rust, how would you declare a C function parameter for a callback that the C side allows to be NULL?
    Use `Option<extern "C" fn(...)>`. The `core::option` docs guarantee that `Option` of a function pointer has the same size and ABI as the pointer, with `None` passed as null. A bare `extern "C" fn` type would claim the pointer is never null, and receiving a null one is undefined behaviour.
  • Why does `Option<*const u8>` take 16 bytes when `Option<NonNull<u8>>` takes 8?
    A raw pointer may legally be null, so it has no forbidden bit pattern for `None` to occupy and `Option` must add a discriminant, which pads out to 16. `NonNull<u8>` promises the pointer is never null, which hands `Option` the zero pattern.
  • Is it safe to rely on `Option<bool>` being 1 byte?
    Not as a layout guarantee. rustc currently stores `None` in an unused `bool` value, but `bool` is not on the documented list in `core::option`, so a future compiler could choose differently. Code that only uses `Option<bool>` normally is unaffected; code that transmutes it or hard-codes its size is relying on a detail.

  • Option always adds a tag byte to its payload
  • Option of a raw pointer is free because pointers can be null
  • Every niche rustc uses today is a documented guarantee
  • Option<u32> is 5 bytes because the tag is one byte
  • The niche optimisation only applies to references
open as a page

In Rust, why is a struct with fields `flag: u8, id: u32, kind: u8` 8 bytes by default but 12 bytes with `#[repr(C)]`?

level: juniorimportance: should knowfreq 45%

basics

~20 s

The default repr(Rust) lets rustc reorder fields, so it can put the u32 first and both u8 fields after it, padding only to 8. repr(C) keeps declaration order, so padding follows each u8 and the size grows to 12.

open as a page

In Rust, why does `size_of::<str>()` fail to compile while `size_of::<&str>()` is 16 and `size_of::<()>()` is 0 on a 64-bit target?

level: middleimportance: should knowfreq 35%

basics

~20 s

str is dynamically sized, and size_of::<T>() requires T: Sized, so rustc reports E0277. A &str is a wide pointer, data address plus length, 16 bytes today. () is a zero-sized type, guaranteed size 0, alignment 1.

open as a page

A Rust `#[repr(C, packed)]` packet header fails with E0793 at `&h.seq` and at `println!("{}", h.len)`: why, and how do you read those fields?

level: seniorimportance: should knowfreq 25%

basics

~20 s

repr(packed) lowers the struct's alignment to 1, so a u32 or u16 field may be misaligned, and even creating a misaligned reference is undefined behaviour. Copy the field out ({ h.len }) or take &raw const and call read_unaligned.

open as a page

In Rust, why mark a newtype like `struct Pid(c_int)` `#[repr(transparent)]` rather than `#[repr(C)]` before passing it to C as an int, and what fields may it hold?

level: seniorimportance: should knowfreq 30%

basics

~20 s

repr(transparent) gives the newtype the same layout and ABI as its single non-trivial field, so C sees a plain int. repr(C) only promises a C struct's layout and ABI. Other fields must be size 0, alignment 1, like PhantomData.

open as a page