W3cubDocs

/Rust

Function std::ptr::read_unaligned

pub unsafe fn read_unaligned<T>(src: *const T) -> T

Reads the value from src without moving it. This leaves the memory in src unchanged.

Unlike read, the pointer may be unaligned.

Safety

Beyond accepting a raw pointer, this is unsafe because it semantically moves the value out of src without preventing further usage of src. If T is not Copy, then care must be taken to ensure that the value at src is not used before the data is overwritten again (e.g. with write, write_bytes, or copy). Note that *src = foo counts as a use because it will attempt to drop the value previously at *src.

Examples

Basic usage:

let x = 12;
let y = &x as *const i32;

unsafe {
    assert_eq!(std::ptr::read_unaligned(y), 12);
}

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html