Skip to content
0:00 / 0:00

#smartpointers#cell#refcell

Crust of Rust: Smart Pointers and Interior Mutability

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 7 people: 7 in engineering communities.
  • A classic. People kept recommending it from 2020 to 2025.
  • Presented by Jon Gjengset

Advanced. Deep dive for people who know the field.

163K views6 years ago7 recommendations

Programming languagesSystems programmingRustMemory management

Jon Gjengset re-implements Cell, RefCell, and Rc from scratch to explain smart pointers and interior mutability in Rust.

In this fourth Crust of Rust video, we cover smart pointers and interior mutability, by re-implementing the Cell, RefCell, and Rc types from the standard library. As part of that, we cover when those types are useful, how they work, and what the equivalent thread-safe versions of these types are. In the process, we go over some of the finer details of Rust's ownership model, and the UnsafeCell type. We also dive briefly into the Drop Check rabbit hole (doc.rust-lang.org/nightly/nomicon/dropck.html) before coming back up for air. This is definitely a more technically advanced stream than some of the earlier ones, and may be a little harder to follow. I apologize for that in advance! Please do leave questions here or on Discord and I'll try to help explain what's going on. You can find the final code at gist.github.com/jonhoo/7cfdfe581e5108b79c2a4e9fbde38de8 and the Discord at discord.gg/RJdqQ9n Introduction Discord Agenda Interior Mutability Cell Trying to Test Cell UnsafeCell RefCell RefCell Smart Pointer Rc (reference counted ptr) NonNull PhantomData and Drop Check ?Sized Briefly Thread Safety Copy-on-Write (Cow) You can watch the live version with comments at youtube.com/watch?v=1e5aDptlGoI

Why it's here

7 people recommend this

What people say. Highly recommended by the community as an essential deep dive for understanding Rust's smart pointers, ownership, and interior mutability.

u/MrTact_actualin r/rust • Recommends

On “When should I use Box, Arc, Rc, Cell and RefCell? Can someone tell me if my usage of these things is correct? I'm trying to measure my understanding of these things as well as my knowledge on borrowing.”

There’s also this great video by @jonhoo where he basically walks through recreating all of these while discussing use cases: link to this video

u/TinBrynin r/rust • Recommends

On “Hey Rustaceans! Got a question? Ask here (6/2023)!”

…ep track of that reference. Edit: What you can do is use `Ref::map` to keep the `RefCell` informed of what is borrowing from it. Playground. This will also mean that you need to drop this `Ref` before you try to get something else that isn't already in the cache or it will panic, but that is what `RefCell` is meant to do. Also Jon Gjengset has a link to this video on implementing his own `RefCell` if you have the time (>2hrs)…

u/phazer99in r/rust • Recommends

On “Common idioms”

I think the most unique thing about Rust is the borrowing and ownership rules. They entail some design patterns not commonly found in other languages. Here some resources to study: * Rust book ownership chapter * Understanding Ownership in Rust * Rust TypeState Builder Pattern * Crust of Rust: Smart Pointers and Interior Mutability

u/davidhuculakin r/rust • Recommends

On “I'm a C++ Programmer trying to learn as much Rust as I can in 5 days.”

Since you're a c++ developer, I think you could get a lot of value quickly by learning how rust approaches smart pointers and interior mutability. It will give some great perspective on how the type system and borrow checker is used to make memory safety be a bit more ergonomic in rust. Jon Gjengsets video on the topic is really excellent: link to this video

u/thvdburgtin r/rust • Recommends

On “What does 'Rc<T> allows only immutable borrows checked at compile time' mean?”

If you have 2 hours to spare, Jon Gjengset has an incredible video about smart pointers and interior mutability, also covering Rc: link to this video

u/wombathqin r/rust • Recommends

On “[Beginner Question] Why and how do we use the heap memory / boxes”

This video helped me quite a bit: link to this video

u/rnottakenin r/rust • Recommends

On “Gave up before shipping a single useful rust app due to high learning curve. Advice?”

I had the same thing when I started, but as many others have mentioned before, at some point the borrow checker just *clicks*. At some point you might need to use smart pointers though, and the video that made that part click for me is this one: link to this video It's a bit more for when you've progressed a bit further in your journey maybe

u/ColdHotdogInhalerin r/rust • Mentions

On “Is UnsafeCell a special magical type known to compiler?”

link to this video