std-method

Compose layouts and inspect field offsets

Extending one layout with another returns both the combined descriptor and the second field offset. Round the final size up with pad_to_align when modeling a C-style aggregate. Do not infer the layout of arbitrary repr(Rust) structures from this calculation. Expected stdout (not executed): offset=4 size=8 Common mistake: Assuming fields can always be placed back-to-back without padding.

Formal Syntax & Structure

rust syntaxrust
layout.extend(other)?.0.pad_to_align()

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::alloc::Layout;    let first = Layout::from_size_align(1, 1).unwrap();    let second = Layout::from_size_align(4, 4).unwrap();    let (combined, offset) = first.extend(second).unwrap();    println!("offset={offset} size={}", combined.pad_to_align().size());}
Rust Reference