Rust Raw Pointers

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • let raw_ptr = &pointee as *const type // create constant raw pointer to some data
  • let raw_mut_ptr = &mut pointee as *mut type // create mutable raw pointer to some mutable data
  • let deref = *raw_ptr // dereference a raw pointer (requires unsafe block)

Remarks

  • Raw pointers are not guaranteed to point to a valid memory address and as such, careless usage may lead to unexpected (and probably fatal) errors.
  • Any normal Rust reference (eg. &my_object where the type of my_object is T) will coerce to *const T. Similarly, mutable references coerce to *mut T.
  • Raw pointers do not move ownership (in contrast to Box values that)


Got any Rust Question?