std-function
Read initialized storage with ptr::read
ptr::read obtains a value while leaving the source bytes unchanged. For Copy types this creates an independent value. For resource-owning types the caller must prevent duplicate ownership and destruction. Prefer ordinary moves or safe cloning when available. Expected stdout (not executed): 21:21 Common mistake: Reading a String this way and allowing both the original and the returned value to be dropped.
Formal Syntax & Structure
rust syntaxrust
unsafe { std::ptr::read(src) }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let value = 21_u16; let src = &raw const value; // SAFETY: live, aligned, initialized u16; shared read; u16 is Copy. let copy = unsafe { std::ptr::read(src) }; println!("{value}:{copy}");}