09
Option
Look at Link from last chapter. Empty or Box<Node>. That's Option<Box<Node>> with the names changed. The standard library's Option already has every helper we hand-rolled with mem::replace and match.
- Drop
Linkentirely.Option<Box<Node>>is the same shape — one tag bit, one pointer payload — and Rust knows the null-pointer optimization, so it's still one machine word.pub struct List { head: Link, } type Link = Option<Box<Node>>; struct Node { elem: i32, next: Link, } -
Option::takeismem::replace(&mut x, None)with a shorter name. It pulls the value out and leavesNonein place. Everymem::replacecall inpush,pop, anddropbecomes a.take().pub fn push(&mut self, elem: i32) { let new_node = Box::new(Node { elem, next: self.head.take(), }); self.head = Some(new_node); } -
Option::mapruns a closure on the inner value if there is one and gives you back a newOption. Our two-armmatchinpopcollapses to a single line.pub fn pop(&mut self) -> Option<i32> { self.head.take().map(|node| { self.head = node.next; node.elem }) }