Skip to content
0:00 / 0:00

#rust#dropcheck#dropck

Crust of Rust: The Drop Check

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Jon Gjengset

Advanced. Deep dive for people who know the field.

29K views5 years ago2 recommendations

Programming languagesSystems programmingRustMemory management

Jon Gjengset explains Rust's drop check mechanism, demonstrating how it prevents undefined behavior when dropping generic types.

In this episode of Crust of Rust, we go over the "drop check" — another niche part of Rust that most people don't have to think about, but which rears its moderately attractive head occasionally when you use generic types in semi-weird ways. In particular, we explore how to implement a Norwegian version of Box (which is really just Box with a different name), and find that the straightforward implementation is not quite as flexible as the standard Box is due to the drop check. When we fix it, we then make it _too_ flexible, and open ourselves the type up to undefined behavior. Which, in turn, we use the drop check to fix. Towards the end, we go through a particularly interesting example at the intersection of the drop check and variance in the form of (ab)using std::iter::Empty. Introduction Boks: A Norwegian Box Freeing a Boks Dereferencing a Boks Boks is too restrictive Boks is not restrictive enough PhantomData and may_dangle What if Oisann didn't touch T? Can I write PhantomData myself? Boks isn't covariant! Boks isn't covariant (in code)! PhantomData T without holding a T std::iter::Empty and variance Why the Empty example compiles Recap: what are all the bits? Unnecessary Complexity? You can read more about subtyping and variance in the Rustonomicon @ doc.rust-lang.org/nomicon/dropck.html. Live version with chat: youtu.be/Das3XSWo1GU

Why it's here

2 people recommend this

What people say. Recommended for understanding Rust's drop check and how implementing Drop affects lifetimes.

u/gusrustin r/rust • Recommends

On “A Borrowck Battle to Remember”

I think the reason arrayvec and vec operated differently in the end is that the arrayvec might not have the dropck eyepatch, more info here: link to this video

u/Waridleyin r/rust • Recommends

On “Why does implementing drop "break" non lexical lifetimes”

It's call the "drop check" link to this video