Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 bytes
  • align_of(): Get type/value alignment
  • drop(): Manually drop a value
  • swap(): Swap two mutable locations

Learn More