Memory Operations
Overview
The std::mem module provides functions for memory inspection and manipulation.
Code Example
#![allow(unused)]
fn main() {
use std::mem;
let value = 42;
let size = mem::size_of_val(&value);
let align = mem::align_of_val(&value);
println!("Value: {}, Size: {} bytes, Alignment: {} bytes", value, size, align);
let mut data = vec![1, 2, 3];
let capacity_before = data.capacity();
data.reserve(10);
let capacity_after = data.capacity();
println!("Capacity before: {}, after: {}", capacity_before, capacity_after);
}
Key Concepts
size_of(): Get type/value size in bytesalign_of(): Get type/value alignmentdrop(): Manually drop a valueswap(): Swap two mutable locations