expression

Write a value using a volatile access

A volatile write requests an observable store without reading or dropping the previous contents. Here an exclusive reference provides writable aligned storage for a Copy integer. The example deliberately uses ordinary RAM, not an actual peripheral. Device registers can have read/write permissions and side effects unlike RAM; obtain those facts from the device manual. Expected host stdout (not executed): 42 Common mistake: Using a volatile write as a replacement for an atomic store or blindly writing reserved register bits.

Formal Syntax & Structure

rust syntaxrust
unsafe { core::ptr::write_volatile(ptr, value) }

Working Example

Example Snippetrust
#![no_std]pub fn store(slot: &mut u32, value: u32) {    // SAFETY: exclusive live u32 storage is writable, aligned and nonoverlapping.    unsafe { core::ptr::write_volatile(slot as *mut u32, value); }}
Rust Reference