Rust

Raw Linked List in Rust

Data Structures

Create a linked list from scratch with unsafe memory, raw pointers, and custom drop guards.

⏱️ 12h 0min
📦 36 modules
Raw Linked List in Rust cover

What You'll Build

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.

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

1

Project Baseline

2

The Node Building Block

3

The LinkedList Container

4

Link Type Alias

5

Test-Driven API Stubs

6

Push Logic

7

Pop Logic

8

Default Trait

9

Shared Iterator Struct

10

Iterator Test Contract

11

Shared Iterator Traversal

12

Mutable Iterator Struct

13

Mutable Iterator Test Contract

14

Mutable Iterator Traversal

15

Owning Iterator Struct

16

Owning Iterator Test

17

IntoIterator Trait

18

Raw Pointers and Unsafe Memory

19

Panic-Safe Drop Guard

20

Tail Pointer

21

Previous Node Link

22

Front-Suffixed API

23

Back Push Test Contract

24

Back Push Logic

25

Back Pop Test Contract

26

Back Pop Logic

27

Reverse Owning Iteration

28

Bidirectional IntoIter Test

29

Length Tracking Field

30

Length and Emptiness API

31

Dual-Cursor Iter Layout

32

Reverse Iter Test

33

Bidirectional Shared Iteration

34

Dual-Cursor IterMut Layout

35

Reverse IterMut Test

36

Bidirectional Mutable Iteration

Technologies

Rust Linked List Data Structures Unsafe Rust Raw Pointers NonNull Iterator DoubleEndedIterator Drop Lifetime PhantomData