[Avg. reading time: 4 minutes]

Memory Layout

How many bytes does a type actually occupy?

The std::mem::size_of::() function helps you inspect the stack size of any type at compile time.

use std::mem;

fn main() {
    println!("i32    : {}", mem::size_of::<i32>());
    println!("u32    : {}", mem::size_of::<u32>());
    println!("f64    : {}", mem::size_of::<f64>());
    println!("char   : {}", mem::size_of::<char>());
    println!("&str   : {}", mem::size_of::<&str>());
    println!("String : {}", mem::size_of::<String>());
}
TypeSize (bytes)Why
i32432 bits = 4 bytes
u32432 bits unsigned
f64864-bit float
char4Unicode scalar value
&str16pointer (8) + length (8)
String24pointer (8) + length (8) + capacity (8)
TypeStack SizeHeap Usage
i324None
f648None
char4None
&str16No Ownership
String24Yes, for text
  • Primitive numeric types store values directly.
  • &str is a pointer containing a memory address and length.
  • String stores metadata on the stack but allocates its text on the heap.

String Literal points to location where Literal values are stored (Static Memory).

fn main(){
    let s = "Rachel";
    println!("{},{:p}", s, s.as_ptr())
}

#memoryVer 2.0.12

Last change: 2026-02-18