# 1.17.0[−][src]Function std::ptr::write_unaligned

    pub unsafe fn write_unaligned<T>(dst: *mut T, src: T)

Overwrites a memory location with the given value without reading or dropping the old value.

Unlike `write`, the pointer may be unaligned.

`write_unaligned` does not drop the contents of `dst`. This is safe, but it could leak allocations or resources, so care should be taken not to overwrite an object that should be dropped.

Additionally, it does not drop `src`. Semantically, `src` is moved into the location pointed to by `dst`.

This is appropriate for initializing uninitialized memory, or overwriting memory that has previously been read with `read_unaligned`.

# Safety

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

- `dst` must be valid for writes.

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

# Examples

Access fields in a packed struct:

    use std::{mem, ptr};

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

    let v = 0x01020304;
    let mut x: Packed = unsafe { mem::zeroed() };

    unsafe {
        // Take a reference to a 32-bit integer which is not aligned.
        let unaligned = &mut x.unaligned as *mut u32;

        // Dereferencing normally will emit an aligned store instruction,
        // causing undefined behavior because the pointer is not aligned.
        // *unaligned = v; // ERROR

        // Instead, use `write_unaligned` to write improperly aligned values.
        ptr::write_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`)