std-method

Validate a custom allocation layout

When an application accepts a requested size and alignment, use the checked constructor. An alignment must be a nonzero power of two, and rounding the size to that boundary must remain representable within isize::MAX. An accepted descriptor still does not guarantee allocation success. Expected stdout (not executed): 13:8 invalid=true Common mistake: Passing an arbitrary alignment such as 3 to an unchecked constructor.

Formal Syntax & Structure

rust syntaxrust
Layout::from_size_align(size, align)

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::alloc::Layout;    let ok = Layout::from_size_align(13, 8).unwrap();    println!("{}:{}", ok.size(), ok.align());    println!("invalid={}", Layout::from_size_align(13, 3).is_err());}
Rust Reference