Generic code and protocols
Use type parameters when several types need the same algorithm or data shape. These lessons build on core functions, borrowing, classes, and enums. Recursive data is an optional extension at the end.
- Replace duplicated functions with a type parameter
- Infer or choose a specialization
- Requiring methods with a protocol
- Parameterize a class
- Parameterize an enum
- Parameterize a protocol
- Store different implementations of a protocol
- Give a method its own type parameters
- Keep a numeric bound in the type
- Calculate with quantities
- Build recursive data
- Use values through a box
- Store recursive children in a list
- Remove a child and clean up a recursive structure
- Traverse recursive data through a borrow
- Reborrow while following a recursive structure
- Preserve a chain when insertion fails
- Represent parent links with node IDs