Formal Equations, Memory Identities & Mathematical Invariants
Review essential mathematical equations, alignment formulas, fat pointer byte identities, and formal theorems governing Rust systems engineering.
Rust's memory safety guarantees are founded on rigorous mathematical logic.
This reference lesson brings together the core formal equations, alignment formulas, memory identities, and theorems established throughout the curriculum.
All standard fat pointers (&[T], &str, dyn Trait) occupy exactly 16 bytes on 64-bit hardware.
1. Hardware Alignment & Padding Equations#
For any composite struct containing field types with alignments :
Aggregate Struct Alignment#
The struct's alignment requirement equals the maximum alignment of its constituent components:
Field Offset Invariant#
Every field offset must be an integer multiple of that field's alignment requirement:
Total Memory Footprint#
Total memory footprint equals the sum of field sizes plus alignment padding bytes () and tail padding ():
2. The XOR Aliasing Invariant#
At any given point in program execution, for any resource , active immutable references and active mutable references must satisfy mutual exclusivity:
3. 64-Bit Memory Size Identities#
Understanding the exact byte footprint of primitives and pointers on 64-bit hardware is essential for cache optimization:
| Type Expression | Size in Bytes | Internal Representation & Components |
|---|---|---|
&T / &mut T | 8 Bytes | Single 64-bit hardware memory pointer. |
*const T / *mut T | 8 Bytes | Bare 64-bit raw pointer address. |
Box<T> | 8 Bytes | Single 64-bit pointer pointing to heap allocation. |
Option<&T> | 8 Bytes | Niche optimization: 0x0 represents None (Zero tag overhead). |
&[T] (Slice Reference) | 16 Bytes | Fat Pointer: 8-byte base address + 8-byte usize element count. |
&str (String Slice) | 16 Bytes | Fat Pointer: 8-byte UTF-8 data address + 8-byte usize byte length. |
dyn Trait (Trait Object) | 16 Bytes | Fat Pointer: 8-byte payload pointer + 8-byte static vtable pointer. |
String | 24 Bytes | 8-byte pointer + 8-byte capacity + 8-byte length. |
Vec<T> | 24 Bytes | 8-byte buffer pointer + 8-byte capacity + 8-byte length. |
() (Unit / ZST) | 0 Bytes | Zero-Sized Type: stripped from physical memory layouts. |
4. The Thread Safety Theorem#
The foundational theorem connecting Send and Sync:
A type is thread-safe to share across concurrent threads if and only if an immutable borrow of () is safe to transfer between threads.
Closure Subtyping Subset Hierarchy#
5. Concept Verification#
Test your mastery of formal equations and memory identities:
What is the exact memory size of an Option<&T> on a 64-bit architecture?
If a struct has three fields with alignment requirements 1 byte, 4 bytes, and 8 bytes, what is the aggregate alignment requirement of the struct?