std::ptr::read_unaligned - Rust
Description: API documentation for the Rust `read_unaligned` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, read_unaligned

# 1.17.0[−][src]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`, `read_unaligned` works with unaligned pointers.

# Safety

Behavior is undefined if any of the following conditions are violated:

- `src` must be valid for reads.

Like `read`, `read_unaligned` creates a bitwise copy of `T`, regardless of whether `T` is `Copy`. If `T` is not `Copy`, using both the returned value and the value at `*src` can violate memory safety.

Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.

# Examples

Access members of a packed struct by reference:

    use std::ptr;

    #[repr(packed, C)]
    struct Packed {
        _padding: u8,
        unaligned: u32,
    }

    let x = Packed {
        _padding: 0x00,
        unaligned: 0x01020304,
    };

    let v = unsafe {
        // Take the address of a 32-bit integer which is not aligned.
        // This must be done as a raw pointer; unaligned references are invalid.
        let unaligned = &x.unaligned as *const u32;

        // Dereferencing normally will emit an aligned load instruction,
        // causing undefined behavior.
        // let v = *unaligned; // ERROR

        // Instead, use `read_unaligned` to read improperly aligned values.
        let v = ptr::read_unaligned(unaligned);

        v
    };

    // Accessing unaligned values directly is safe.
    assert!(x.unaligned == v);Run

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)