Masai Mahapa

30 days of Rust - Day Seventeen - Smart Pointers Continued

30 days of rust - day Seventeen

Good artists borrow, great artists steal. By Pablo Picasso

I am a huge fan of Medium. I read a lot of content from there. I wanted to share my blogs on their platform but I have one issue. "The PayWall".

Being a self taught software developer, means I have relied a lot on free content online to grow to the point I am today. I believe the same opportunity should be afforded to many other people like me. So I want my content to be free forever.

What does this have to do with artists stealing?

Medium has a listen feature. So yesterday, I decided to code the feature to my blog posts which allows you to listen to my writing. It's experimental and I hope to improve it over time. Please click the Listen button at the top and let me know what you think of it.

Currently there is no functionality to pause. You can only play and stop (which starts from the beginning next time you play).

Day 17 - Smart Pointers Continued

Please see my blog post from yesterday introducing smart pointers.

A trait important to the smart pointer is the Drop trait which lets us decide what happens when a value is about to go out of scope. If you're a React developer, this is similar to componentWillUnmount.

This trait can be applied on any type and the code can be used to free up resources such as files or network connections.

The Drop trait requires us to implement one method called drop.

struct CustomSmartPointer {
   data: String,
}
 
impl Drop for CustomSmartPointer {
   fn drop(&mut self) {
       println!("Dropping CustomSmartPointer with data `{}`!", self.data);
   }
}
 
fn main() {
   let c = CustomSmartPointer {
       data: String::from("my stuff"),
   };
   let d = CustomSmartPointer {
       data: String::from("other stuff"),
   };
   println!("CustomSmartPointers created.");
} //c and d go out of scope

The output will look like;

CustomSmartPointers created.
Dropping CustomSmartPointer with data `other stuff`!
Dropping CustomSmartPointer with data `my stuff`!

RC<T> - Reference Counted Smart Pointer

This is used to allow for multiple owners of a piece of data. Remember as discussed on day 2 regarding ownership, Rust does not allow this behavior by default. This smart pointer type just keeps track of how many references there are to a value.

Think about it like a TV in your house. When a person walks in they can turn on the TV. There can be more than one person who can watch the TV at the same time. Once the last person goes out of the TV room, they switch the TV off. If the TV is turned off while people are watching the soccer game, all hell shall break loose.

This is exactly how this smart pointer works. Once the last reference goes out of scope, then this piece of data also gets dropped.

enum List {
   Cons(i32, Rc),
   Nil,
}
 
use List::{Cons, Nil};
use std::rc::Rc;
 
fn main() {
   let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
   println!("count after creating a = {}", Rc::strong_count(&a));
   let b = Cons(3, Rc::clone(&a));
   println!("count after creating b = {}", Rc::strong_count(&a));
   {
       let c = Cons(4, Rc::clone(&a));
       println!("count after creating c = {}", Rc::strong_count(&a));
   }
   println!("count after c goes out of scope = {}", Rc::strong_count(&a));
}

The output of this code will be;

count after creating a = 1
count after creating b = 2
count after creating c = 3
count after c goes out of scope = 2

As shown in the output, once a is created, there's just one reference. After b is created by cloning the reference to a we then have 2 references being counted. c is also a clone to a's reference which goes to 3, and soon after c goes out of scope and our reference count is back to 2.

Conclusion

This should be enough smart pointer theory to get going. I can always come back for more as the need arises.

Smart pointers are just like your home address with just a little bit of added functionality and metadata. This understanding only should go a long way to creating better software in and out of Rust.

Please let me know what you think of this series and how I can supercharge my learning. It would also be nice to know what you are currently upskilling on.

Thanks for spending some time learning Rust with me. Until next time. Peace ✌🏼 .

Share