How to use transmute safely

Use std::mem::transmute inside an unsafe block only when you guarantee identical memory layouts between types.

Use std::mem::transmute only when you are certain the source and destination types have identical memory layouts and lifetimes, as it bypasses all safety checks. Wrap the operation in an unsafe block and ensure the types are #[repr(C)] or #[repr(transparent)] to guarantee layout compatibility.

use std::mem;

#[repr(C)]
struct Point { x: f64, y: f64 }

fn main() {
    let point = Point { x: 1.0, y: 2.0 };
    // Only safe if Point and [f64; 2] have identical layout
    let arr: [f64; 2] = unsafe { mem::transmute(point) };
}