Pointer dereference desugaring

⚓ Rust    📅 2025-09-30    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 31 days ago. The information described in this article may have changed.

Some unsafe code that dereferences a raw pointer is equivalent to code using pointer methods:

  • unsafe { *ptr } is the same as unsafe { ptr.read() }

  • unsafe { *ptr = v; } is the same as unsafe { ptr.drop_in_place(); ptr.write(v); }

  • &*ptr is ptr.as_ref().unwrap_unchecked()

  • &mut *ptr is ptr.as_mut().unwrap_unchecked()

But I wonder if &mut (*ptr).field has an exact equivalent using methods, and more generally what syntactic patterns don't.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed