std-function
Transmute only when both types admit the bit pattern
Transmutation moves a representation between types of equal size. Size alone cannot establish destination validity. This narrow illustration uses fixed-width integers for which every bit pattern is a valid value. In real code use a normal integer cast for this conversion. Expected stdout (not executed): 65535 Common mistake: Transmuting an arbitrary byte to bool because both types occupy one byte.
Formal Syntax & Structure
rust syntaxrust
unsafe { std::mem::transmute::<i16, u16>(value) }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let signed = -1_i16; // SAFETY: i16 and u16 have equal size, no padding, and all their bit patterns are valid. let unsigned = unsafe { std::mem::transmute::<i16, u16>(signed) }; println!("{}", unsigned);}