Project 2: Implement a Reference-Counted Smart Pointer (Rc)

Your own version of Rc<T> (reference-counted pointer) that allows multiple ownership through runtime reference counting.

Quick Reference

Attribute Value
Primary Language Rust
Alternative Languages C++ (std::shared_ptr for comparison)
Difficulty Level 3: Advanced
Time Estimate 1 week
Knowledge Area Smart Pointers / Memory Management
Tooling Rust, unsafe
Prerequisites Project 1, understanding of ownership

What You Will Build

Your own version of Rc<T> (reference-counted pointer) that allows multiple ownership through runtime reference counting.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Implementing reference counting with interior mutability → maps to Cell/RefCell patterns
  • Handling cloning and dropping → maps to incrementing/decrementing counts correctly
  • Preventing memory leaks from cycles → maps to understanding Rc’s limitations
  • Using unsafe to implement safe abstractions → maps to encapsulating unsafe code

Key Concepts

  • Smart Pointers: “The Rust Programming Language” Chapter 15
  • Interior Mutability: “Programming Rust” Chapter 9
  • Reference Counting: “The Rustonomicon” - Ownership chapter
  • Rc Implementation: Study std::rc::Rc source code

Real-World Outcome

Deliver a working demo with observable output that proves the feature is correct.


Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. 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
  • “Programming Rust” by Blandy, Orendorff, Tindall