Member-only story

Smart Pointers in Rust — RC<T> type explained (Reference Counted )

Shanmukh Sista
6 min readApr 13, 2023

It was very daunting for me initially to see Rc , Arc , being used in almost every open source repository I explored. I started reading books and tried to understand the underlying concepts behind Reference counting and Garbage collection, and it took a while for me to grasp the basics. This article is my attempt on explaining Rc , a reference counted wrapper type in Rust with a few examples I wrote.

Before we dive deep into Rc types, let’s look at a simple rust program that tries to model a common real world scenario — An author can publish ultiple books.

Starter Program

This program tries to create two books , and have a common author for these books. I started writing the structure as if I were writing any other programming language to understand the errors.

struct Author {
name : String
}

impl Author {
fn new (name : String) -> Self {
return Author{name }
}
}

struct Book {
name : String,
author : Author
}

impl Book{
fn new (name : String, author : Author) -> Self{
Book{name , author}
}
}

fn main() {
let author = Author::new("J.K. Rowling".to_owned())
let chamber_of_secrets = Book::new(
"Harry Potter & The Chamber of secrets".to_owned()

Responses (1)

Write a response