Function align_of_val

#[must_use]
pub const fn align_of_val<T: ?Sized>(val: &T) -> usize

Returns the ABI-required minimum alignment of the type of the value that val points to, in bytes.

This function is identical to [align_of::<T>()][align_of] whenever T: [Sized], but also supports determining the alignment required by a dyn Trait value, which is the alignment of the underlying concrete type.

Examples

assert_eq!(4, align_of_val(&5i32));

(Caution: it is not guaranteed that the alignment of i32 is 4; that is, this example assertion does not pass on all platforms.)

dyn types may have different alignments for different values; align_of_val can be used to learn those alignments:

let a: &dyn ToString = &1234u16;
let b: &dyn ToString = &String::from("abcd");

assert_eq!(align_of_val(a), align_of::<u16>());
assert_eq!(align_of_val(b), align_of::<String>());