Project 7: Implement a Self-Referential Struct with Pin
A self-referential struct (e.g., a struct containing a String and a &str pointing into it) using Pin to prevent it from being moved.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | Rust |
| Alternative Languages | None |
| Difficulty | Level 5: Master |
| Time Estimate | 2-3 weeks |
| Knowledge Area | Unsafe Rust / Pin API |
| Tooling | Rust std::pin |
| Prerequisites | All previous projects, deep Rust understanding |
What You Will Build
A self-referential struct (e.g., a struct containing a String and a &str pointing into it) using Pin to prevent it from being moved.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Creating a self-referential struct → maps to understanding why they’re normally forbidden
- Using Pin to prevent moves → maps to
Pin<Box<T>>and!Unpin - Implementing safe projections → maps to pin-project crate patterns
- Understanding async/await connection → maps to why futures need Pin
Key Concepts
- Pin and Unpin: “Asynchronous Programming in Rust” book
- Self-Referential Structs: “The Rustonomicon” - Self-referential structs chapter
- Pin RFC: RFC 2349 documentation
- pin-project: Study this crate’s macros and documentation
Real-World Outcome
Deliver a working demo with observable output that proves the feature is correct.
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
RUST_BORROW_CHECKER_LIFETIME_PHILOSOPHY.md - “The Rustonomicon” + Pin RFC