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

# 1.9.0[−][src]Function std::ptr::read_volatile

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

Performs a volatile read of the value from `src` without moving it. This leaves the memory in `src` unchanged.

Volatile operations are intended to act on I/O memory, and are guaranteed to not be elided or reordered by the compiler across other volatile operations.

Memory accessed with `read_volatile` or `write_volatile` should not be accessed with non-volatile operations.

# Notes

Rust does not currently have a rigorously and formally defined memory model, so the precise semantics of what "volatile" means here is subject to change over time. That being said, the semantics will almost always end up pretty similar to C11's definition of volatile.

The compiler shouldn't change the relative order or number of volatile memory operations. However, volatile memory operations on zero-sized types (e.g., if a zero-sized type is passed to `read_volatile`) are no-ops and may be ignored.

# Safety

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

- `src` must be valid for reads.

- `src` must be properly aligned.

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. However, storing non-`Copy` types in volatile memory is almost certainly incorrect.

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

Just like in C, whether an operation is volatile has no bearing whatsoever on questions involving concurrent access from multiple threads. Volatile accesses behave exactly like non-atomic accesses in that regard. In particular, a race between a `read_volatile` and any write operation to the same location is undefined behavior.

# Examples

Basic usage:

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

    unsafe {
        assert_eq!(std::ptr::read_volatile(y), 12);
    }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`)