expression
Measure padding without reading padding bytes
Padding makes the next field or array element meet alignment requirements. This example explicitly requests alignment four, so it can observe unused space without reading any bytes of that space. Padding need not contain initialized data. Expected stdout (not executed): padding=3 Common mistake: Casting an entire struct into a byte slice and assuming every padding byte is initialized.
Formal Syntax & Structure
rust syntaxrust
size_of::<T>() - sum_of_field_sizesWorking Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { use std::mem::size_of; #[repr(C, align(4))] struct Record { code: u8 } let padding = size_of::<Record>() - size_of::<u8>(); println!("padding={padding}");}