memtrace
PlaygroundBetaLearn memoryFollow on

Getting started

  • Introduction

Unsafe Rust

  • Introduction
  • Raw pointers
  • Undefined behavior

Real world

  • Cloudflare: 100 TB saved in 1.1.1.1's DNS cache

Playground

  • How the Playground works
  • What the Playground can't do yet

Unsafe Rust

Raw pointers

Addresses without borrow tracking: what they are, and why reading through one needs unsafe

What a raw pointer is

A raw pointer is just an address. Unlike a reference it can be null, dangling or aliased, so creating one is safe but reading or writing through it needs unsafe

Writing through a pointer

1fn main() {
2 let mut x = 5;
3 let p = &raw mut x;
4
5 unsafe {
6 *p += 1;
7 }
8 println!("{x}");
9}
$6Visualize in Playground
STACKone frame per callHEAPallocated at runtimeNo allocationsmain()main()runningrunningp: *mut i32 = → xp: *mut i32→ xx: i32 = 6x: i326
p holds the address of x. Writing through it changes x in place, with no borrow tracking
PreviousUnsafe RustNextUndefined behavior

On this page

  1. What a raw pointer is
  2. Writing through a pointer