Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Idiomatic Rust code guidance based on Apollo GraphQL's best practices handbook for ownership, errors, and performance.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/chapter_09.md
1# Chapter 9 - Understanding Pointers23Many higher level languages hide memory management, typically **passing by value** (copy data) or **passing by reference** (reference to shared data) without worrying about allocation, heap, stack, ownership and lifetimes, it is all delegated to the garbage collector or VM. Here is a comparison on this topic between a few languages:45### ๐ Language Comparison67| Language | Value Types | Reference/Pointer Types | Async Model & Types | Manual Memory |8|------------ |------------------------------------- |----------------------------------------------------------- |---------------------------------------------------------------------------- |------------------------------ |9| Python | None | Everything is a reference | async def, await, Task, coroutines and asyncio.Future | โ Not Allowed |10| Javascript | Primitives | Objects | `async/await`, `Promise`, `setTimeout`. single threaded event loop | โ Not Allowed |11| Java | Primitives | Objects | `Future<T>`, threads, Loom (green threads) | โ Almost none & not recommended |12| Go | Values are copied unless using `&T` | Pointers (`*T`, `&T`), escape analysis | goroutines, `channels`, `sync.Mutex`, `context.Context` | โ ๏ธ Limited |13| C | Primitives and structs supported | Raw pointers `T*` and `*void` | Threads, event loops (`libuv`, `libevent`) | โ Fully |14| C++ | Primitives and references | Raw `T*` and smart pointers `shared_ptr` and `unique_ptr` | threads, `std::future`, `std::async`, (since c++ 20 `co_await/coroutines`) | โ Mostly |15| Rust | Primitives, Arrays, `impl Copy` | `&T`, `&mut T`, `Box<T>`, `Arc<T>` | `async/await`, `tokio`, `Future`, `JoinHandle`, `Send + Sync` | โ ๐ Safe and Explicit |1617## 9.1 Thread Safety1819Rust tracks pointers using `Send` and `Sync` traits:20- `Send` means data can move across threads.21- `Sync` means data can be referenced from multiple threads.2223> A pointer is thread-safe only if the data behind it is.2425| Pointer Type | Short Description | Send + Sync? | Main Use |26|---------------- |--------------------------------------------------------------------------- |-------------------------------------- |------------ |27| `&T` | Shared reference | Yes | Shared access |28| `&mut T` | Exclusive mutable reference | No, not Send | Exclusive mutation |29| `Box<T>` | Heap-allocated owning pointer | Yes, if T: Send + Sync | Heap allocation |30| `Rc<T>` | Single-threaded ref counted pointer | No, neither | Multiple owners (single-thread) |31| `Arc<T>` | Atomic ref counter pointer | Yes | Multiple owners (multi-thread) |32| `Cell<T>` | Interior mutability for copy types | No, not Sync | Shared mutable, non-threaded |33| `RefCell<T>` | Interior mutability (dynamic borrow checker) | No, not Sync | Shared mutable, non-threaded |34| `Mutex<T>` | Thread-safe interior mutability with exclusive access | Yes | Shared mutable, threaded |35| `RwLock<T>` | Thread-safe shared readonly access OR exclusive mutable access | Yes | Shared mutable, threaded |36| `OnceCell<T>` | Single-thread one-time initialization container (interior mutability ONCE) | No, not Sync | Simple lazy value initialization |37| `LazyCell<T>` | A lazy version of `OnceCell<T>` that calls function closure to initialize | No, not Sync | Complex lazy value initialization |38| `OnceLock<T>` | Thread-safe version of `OnceCell<T>` | Yes | Multi-thread single init |39| `LazyLock<T>` | Thread-safe version of `LazyCell<T>` | Yes | Multi-thread complex init |40| `*const T/*mut T` | Raw Pointers | No, user must ensure safety manually | Raw memory / FFI |4142## 9.2 When to use pointers:4344### `&T` - Shared Borrow:4546Probably the most common type in a Rust code base, it is **Safe, with no mutation** and allows **multiple readers**.4748```rust49let data: String = String::from_str("this a string").unwrap();5051print_len(&data);52print_capacity(&data);53print_bytes(&data);5455fn print_len(s: &str) {56println!("{}", s.len())57}5859fn print_capacity(s: &String) {60println!("{}", s.capacity())61}6263fn print_bytes(s: &String) {64println!("{:?}", s.as_bytes())65}66```67### `&mut T` - Exclusive Borrow:6869Probably the most common *mutable* type in a Rust code base, it is **Safe, but only allows one mutable borrow at a time**.7071```rust72let mut data: String = String::from_str("this a string").unwrap();73mark_update(&mut data);7475fn mark_update(s: &mut String) {76s.push_str("_update");77}78```7980### [`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html) - Heap Allocated8182Single-owner heap-allocated data, great for recursive types and large structs.8384```rust85pub enum MySubBoxedEnum<T> {86Single(T),87Double(Box<MySubBoxedEnum<T>>, Box<MySubBoxedEnum<T>>),88Multi(Vec<T>), // Note that Vec is already a boxed value89}90```9192### [`Rc<T>`](https://doc.rust-lang.org/std/rc/struct.Rc.html) - Reference Counter (single-thread)9394You need multiple references to data in a single thread. Most common example is linked-list implementation.9596### [`Arc<T>`](https://doc.rust-lang.org/std/sync/struct.Arc.html) - Atomic Reference Counter (multi-thread)9798You need multiple references to data in multiple threads. Most common use cases is sharing readonly Vec across thread with `Arc<[T]>` and wrapping a `Mutex` so it can be easily shared across threads, `Arc<Mutex<T>>`.99100### [`RefCell<T>`](https://doc.rust-lang.org/std/cell/struct.RefCell.html) - Runtime checked interior mutability101102Used when you need shared access and the ability to mutate date, borrow rules are enforced at runtime. **It may panic!**.103104```rust105use std::cell::RefCell;106let x = RefCell::new(42);107*x.borrow_mut() += 1;108109assert_eq!(&*x.borrow(), 42, "Not meaning of life");110```111112Panic example:113```rust114use std::cell::RefCell;115let x = RefCell::new(42);116117let borrow = x.borrow();118119let mutable = x.borrow_mut();120```121122### [`Cell<T>`](https://doc.rust-lang.org/std/cell/struct.Cell.html) - Copy-only interior mutability123124Somewhat the fast and safe version of `RefCell`, but it is limited to types that implement the `Copy` trait:125126```rust127use std::cell::Cell;128129struct SomeStruct {130regular_field: u8,131special_field: Cell<u8>,132}133134let my_struct = SomeStruct {135regular_field: 0,136special_field: Cell::new(1),137};138139let new_value = 100;140141// ERROR: `my_struct` is immutable142// my_struct.regular_field = new_value;143144// WORKS: although `my_struct` is immutable, `special_field` is a `Cell`,145// which can always be mutated with copy values146my_struct.special_field.set(new_value);147assert_eq!(my_struct.special_field.get(), new_value);148```149150### [`Mutex<T>`](https://doc.rust-lang.org/std/sync/struct.Mutex.html) - Thread-safe mutability151152An exclusive access pointer that allows a thread to read/write the data contained inside. It is usually wrapped in an `Arc` to allow shared access to the Mutex.153154### [`RwLock<T>`](https://doc.rust-lang.org/std/sync/struct.RwLock.html) - Thread-safe mutability155156Similar to a `Mutex`, but it allows multiple threads to read it OR a single thread to write. It is usually wrapped in an `Arc` to allow shared access to the RwLock.157158159### [`*const T/*mut T`](https://doc.rust-lang.org/std/primitive.pointer.html) - Raw pointers160161Inherently **unsafe** and necessary for FFI. Rust makes their usage explicit to avoid accidental misuse and unwilling manual memory management.162163```rust164let x = 5;165let ptr = &x as *const i32;166unsafe {167println!("PTR is {}", *ptr)168}169```170171### [`OnceCell`](https://doc.rust-lang.org/std/cell/struct.OnceCell.html) - Single-thread single initialization container172173Most useful when you need to share a configuration between multiple data structures.174175```rust176use std::{cell::OnceCell, rc::Rc};177178#[derive(Debug, Default)]179struct MyStruct {180distance: usize,181root: Option<Rc<OnceCell<MyStruct>>>,182}183184fn main() {185let root = MyStruct::default();186let root_cell = Rc::new(OnceCell::new());187if let Err(previous) = root_cell.set(root) {188eprintln!("Previous Root {previous:?}");189}190let child_1 = MyStruct{191distance: 1,192root: Some(root_cell.clone())193};194195let child_2 = MyStruct{196distance: 2,197root: Some(root_cell.clone())198};199200201println!("Child 1: {child_1:?}");202println!("Child 2: {child_2:?}");203}204```205206### [`LazyCell`](https://doc.rust-lang.org/std/cell/struct.LazyCell.html) - Lazy initialization of `OnceCell`207208Useful when the initialized data can be delayed to when it is actually being called.209210### [`OnceLock`](https://doc.rust-lang.org/std/sync/struct.OnceLock.html) - thread-safe `OnceCell`211212Useful when you need a `static` value.213214```rust215use std::sync::OnceLock;216217static CELL: OnceLock<u32> = OnceLock::new();218219// `OnceLock` has not been written to yet.220assert!(CELL.get().is_none());221222// Spawn a thread and write to `OnceLock`.223std::thread::spawn(|| {224let value = CELL.get_or_init(|| 12345);225assert_eq!(value, &12345);226})227.join()228.unwrap();229230// `OnceLock` now contains the value.231assert_eq!(232CELL.get(),233Some(&12345),234);235```236237### [`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) - thread-safe `LazyCell`238239Similar to `OnceLock`, but the static value is a bit more complex to initialize.240241```rust242use std::sync::LazyLock;243244static CONFIG: LazyLock<HashMap<&str, T>> = LazyLock::new(|| {245let data = read_config();246let mut config: HashMap<&str, T> = data.into();247config.insert("special_case", T::default());248config249});250251let _ = &*CONFIG;252```253254## References255- [Mara Bos - Rust Atomics and Locks](https://marabos.nl/atomics/)256- [Semicolon video on pointers](https://www.youtube.com/watch?v=Ag_6Q44PBNs)257