Pointers¶
Level: 201 · working knowledge
One line: A memory address is a number, a pointer is an address with a type, and a reference is a pointer with promises the compiler checks — this section measures each layer, the second word some pointers carry, the alignment every reference promises, and the raw pointers that promise nothing.
The section follows the vocabulary of Rust in Action, chapter 6 ("Memory"), which is also the source whose claims it checks:
| Word | Say it when | Rust types |
|---|---|---|
| reference | the compiler's guarantees apply | &T, &mut T |
| pointer | talking about the primitive idea, where you are responsible | any of these |
| raw pointer | the unsafety should be explicit | *const T, *mut T |
| smart pointer | a type that acts like a pointer and manages what it points at | Box<T>, Rc<T>, String, … — its own section |
Rust's Reference groups them the same way under Pointer types ↗. Where a pointer is two words, this section calls it wide, the Reference's word, rather than fat.
Lessons¶
| Lesson | Level | What it answers |
|---|---|---|
| Address, pointer, reference | 201 | What each of the three adds to the one before — a type, provenance, and five promises — and why a pointer made from a number is not the pointer it equals |
| Wide pointers | 201 | When a pointer is two words, why the second is a length for slices and a vtable pointer for dyn Trait, and why wide rather than fat |
A reference is aligned to its referent, not to usize |
201 → 301 | What alignment belongs to, where padding comes from, why repr(packed) fields refuse a reference, and what a misaligned one does in debug, -O and Miri |
Raw pointers: *const T and *mut T |
201 → 301 | What safe code may do with one, what needs unsafe, the aliasing rules the borrow checker does not check, and reading from address 0 |
| Rust in Action, chapter 6: pointer claims, run | 201 | Seventeen claims from the chapter and its listings, checked on rustc 1.98.0 — including a listing that is undefined behaviour |
Read them in that order: the first names the three layers, the next two measure what a pointer's value holds and where it may point, the fourth takes the promises away, and the last is the chapter the section was written against.
Already elsewhere in the library¶
These pages were written before this section, and each answers one pointer question in place. They are linked, not repeated:
| Question | Page |
|---|---|
Which address does &x give you, and why does no example print one? |
What an address shows |
What does the lifetime in &'a T promise? |
What &'a T claims |
Why can there be many &T or one &mut T? |
Borrowing and Reborrowing |
| How does the compiler decide a borrow is refused? | A borrow is a loan, and every page on references in order: References: the map |
&T, &mut T, *const T, *mut T, Box<T> and Rc<T> side by side? |
Six pointer types, one table |
| How is "no value" written without a null? | Nullable pointers and Option is a one-item collection |
Why is &str two words? |
str is unsized and Step 2: Some types have no size |
What does a String's pointer point at? |
The anatomy of a String |
What does unsafe allow, and what does it not turn off? |
What unsafe turns off |
| What is a pointer to a function? | Function pointers |
What does a dyn Trait pointer cost at a call? |
Static vs dynamic dispatch |
| What do the C pointer bugs look like, and what does Rust refuse? | Null dereference, Use-after-free, Double free, Buffer overruns |
| How do you build an owner from a raw pointer? | Vec::from_raw_parts and String::from_raw_parts |
| Where does heap memory come from? | The global allocator and Allocator::shrink |
Words¶
Every term below has a glossary entry: memory address, raw pointer, provenance, wide pointer (and fat pointer, the older name), thin pointer, pointer alignment, padding, pointer cast, NonNull<T>, niche.
See also¶
- Smart pointers — the section after this one:
Deref,Drop, and whatBoxandRccost - Ownership — the rules references exist to follow
- C and C++ — the bugs raw pointers make possible, run in C
- Books — Rust in Action among the others
Po polsku¶
Ten dział mierzy trzy warstwy: adres (liczba), wskaźnik (adres z typem i pochodzeniem — provenance) oraz referencję (wskaźnik z obietnicami, które sprawdza kompilator). Nazewnictwo idzie za Rust in Action: „referencja”, gdy działają gwarancje kompilatora, „wskaźnik”, gdy odpowiadasz sam, „surowy wskaźnik”, gdy tę niebezpieczność trzeba nazwać wprost. Wskaźnik złożony z dwóch słów nazywamy szerokim (wide), jak Reference, a nie „grubym” (fat).
Lekcje odpowiadają po kolei: co dodaje każda warstwa; kiedy wskaźnik ma drugie słowo i czym ono jest (długością albo wskaźnikiem do vtable); do czego wyrównana jest referencja (do typu, na który wskazuje, nie do usize); co wolno z surowym wskaźnikiem bez unsafe, a co nie; i które twierdzenia rozdziału 6 książki się nie sprawdzają. Wiele pokrewnych stron istniało już wcześniej w innych działach — tabela „Already elsewhere” prowadzi do nich zamiast je powtarzać.
Szukaj po polsku: wskaźniki w Ruście · referencja a wskaźnik · surowe wskaźniki · wyrównanie pamięci · rust pointers references raw pointers