expression
Write through a uniquely derived raw pointer
A raw pointer can write into valid writable storage when the access respects exclusivity. This example derives the pointer from the sole mutable borrow, performs one write, and then uses the original binding after the pointer is no longer needed. Expected stdout (not executed): 9 Common mistake: Using a mutable raw pointer to write memory that is only shared or immutable.
Formal Syntax & Structure
rust syntaxrust
let p: *mut T = &mut value; unsafe { *p = replacement; }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { let mut value = 5_i32; let p: *mut i32 = &mut value; // SAFETY: p originates from the exclusive borrow of live value; // there are no other accesses until the write finishes. unsafe { *p = 9; } println!("{value}");}