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.
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
Assembly Steps
Project Baseline
The Node Building Block
The LinkedList Container
Link Type Alias
Test-Driven API Stubs
Push Logic
Pop Logic
Default Trait
Shared Iterator Struct
Iterator Test Contract
Shared Iterator Traversal
Mutable Iterator Struct
Mutable Iterator Test Contract
Mutable Iterator Traversal
Owning Iterator Struct
Owning Iterator Test
IntoIterator Trait
Raw Pointers and Unsafe Memory
Panic-Safe Drop Guard
Tail Pointer
Previous Node Link
Front-Suffixed API
Back Push Test Contract
Back Push Logic
Back Pop Test Contract
Back Pop Logic
Reverse Owning Iteration
Bidirectional IntoIter Test
Length Tracking Field
Length and Emptiness API
Dual-Cursor Iter Layout
Reverse Iter Test
Bidirectional Shared Iteration
Dual-Cursor IterMut Layout
Reverse IterMut Test
Bidirectional Mutable Iteration