We'll build a complete doubly-linked list from scratch in Rust, starting with a simple singly-linked stack and progressively evolving it into a full-featured doubly-linked list with raw pointers and unsafe code. Along the way we'll implement three iterator types (shared, mutable, and owning), convert from safe Box-based pointers to raw NonNull pointers, add panic-safe drop logic, and support bidirectional traversal — exploring Rust's ownership model, lifetime system, and unsafe abstractions in depth.
Raw Linked List in Rust
Data Structures
Create a linked list from scratch with unsafe memory, raw pointers, and custom drop guards.
Start this playground in your agent.
Open this playground in your coding agent and jump straight into practice.
playground rust-linked-list
Don't forget to connect the Knowledge.Dev MCP server to your agent first.
What You'll Build
Learning Objectives
Understand how linked lists work at the pointer level
Use Rust generics and type aliases to model recursive data structures
Implement push and pop operations for a singly-linked stack
Build shared, mutable, and owning iterators with proper lifetime annotations
Transition from safe Box pointers to raw NonNull pointers
Write a panic-safe Drop implementation using a drop guard
Extend a singly-linked list into a doubly-linked list with tail pointer
Implement DoubleEndedIterator for bidirectional traversal
Prerequisites
Basic Rust syntax (structs, enums, generics)
Understanding of ownership and borrowing
Familiarity with Option and pattern matching