Member-only story
Rustlings: rc1.rs #Issue83 — Smart Pointers in Rust
Rustlings Challenge: rc1.rs Solution Walkthrough
This is the Eighty-third (83rd) issue of the Rustlings series. In this issue, we provide solutions to Rustlings exercises along with detailed explanations. In this issue we will solve the challenge on rc1.rs.
Previous challenge #Issue 82
In Rust, smart pointers are variables that contain an address in memory and reference some other data, but they also have additional metadata and capabilities. Smart pointers in Rust often own the data they point to, while references only borrow data.
Rc<T> stands for Reference Counted, and it’s a smart pointer in Rust. It’s used to enable multiple ownership of data by keeping track of how many references there are to a given piece of data and allowing shared ownership without the need for explicit lifetimes or borrowing rules.
Here’s a basic example of using Rc<T>:
use std::rc::Rc;
fn main() {
let data = Rc::new(vec![1, 2, 3])…