02
New
A type without methods is a record, not an interface. We attach methods through impl blocks. The first one is a constructor.
- Methods live in
impl List { ... }. A function with noselfis an associated function — call it asList::new(), the same way you'd call a static method.impl List { pub fn new() -> Self { List { head: Link::Empty } } } -
Self(capital S) is shorthand for the type theimplblock is on. Use it everywhere instead of repeatingList; renaming the type later only touches the linepub struct Listand not the constructor.let list = List::new(); // list.head is Link::Empty