Skip to content
0:00 / 0:00

#subtyping#variance#lifetimes

Crust of Rust: Subtyping and Variance

Jon Gjengset

111K subscribers • 38 videos on Hallway

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

Advanced. Deep dive for people who know the field.

78K views5 years ago8 recommendations

Programming languagesSystems programmingRustMemory management

Jon Gjengset explains subtyping and variance in Rust, demonstrating how they affect borrow ergonomics and lifetimes through a strtok implementation.

In this episode of Crust of Rust, we go over subtyping and variance — a niche part of Rust that most people don't have to think about, but which is deeply ingrained in some of Rust's borrow ergonomics, and occasionally manifests in confusing ways. In particular, we explore how trying to implement the relatively straightforward `strtok` function from C/C++ in Rust quickly lands us in a place where the function is more or less impossible to call due to variance! Introduction Practical variance in strtok A simple strtok test Implementing strtok Why can't we call strtok? Pretending to be the compiler Shortening lifetimes Subtypes Covariance Contravariance Invariance &'a mut T covariance in 'a What went wrong in our strtok test? Fixing strtok Why is 'b: 'a not needed? Shortening &'a mut and NLL Is 'b: 'a implied for &'a &'b? Variance, PhantomData, and drop check Reasons for changing variance for{'a} and variance Mutating through *const T NonNull{T} How we got here You can read more about subtyping and variance in the Rust reference @ doc.rust-lang.org/nightly/reference/subtyping.html or in the Nomicon @ doc.rust-lang.org/nomicon/subtyping.html. Live version with chat: youtu.be/--wzpkTuCrA

Why it's here

8 people recommend this

What people say. Highly recommended as the definitive explanation of subtyping, variance, and lifetime issues in Rust.

Moments people linked to

u/Worried_Wrongdoer_35in r/rust • Recommends

On “What was the most difficult topic for you to understand while learning rust?”

I found subtyping and variance somewhat difficult and learned it over time from nomicon and video by Jon Gjengset

u/TheGreaT1803in r/rust • Recommends

On “Thank you to whoever updated the "Subtyping and Variance" section of the Rustonomicon.”

Not sure if this counts as stating the obvious, but Jon's video on Subtyping and Variance is also an excellent resource for someone trying to understand this concept [video

u/riasthebestgirlin r/rust • Recommends

On “So, everyone falls in love with Rust at first sight?”

This stream explains it really well

u/JonhooRust for Rustaceansin r/rust • Recommends

On “Uncovered Intermediate Topics”

Something like link to this video ?

u/itamonsterin r/rust • Recommends

On “Why is &mut x active through out main, it shouldn't be active after last reference?”

link to this video In this video he implements strtok and explains in great detail the exact problem you are facing

u/freax13in r/rust • Recommends

On “How is 'static subtype of any lifetime 'a and not supertype?”

Jon Gjengset made a good video about subtyping and variance: link to this video

u/wezmAllsortsin r/rust • Recommends

On “what is subtyping and variance in layman's terms?”

I don’t think you need to be worried about it in your day-to-day programming. Anyway, Jon Gjengset did a video on them, perhaps that will help: link to this video

u/kinoshitajonain r/rust • Recommends

On “Why is &mut x active through out main, it shouldn't be active after last reference?”

Your code is exactly the same as this video: link to this video Did you not also listen to Jon's verbal explanation in the video? He does a really good job of explaining it, you should really listen to it all the way to the end.

u/TheDutchMC76in r/rust • Mentions

On “Rust traits and "+ 'static"”

/u/jonhoo has a great one on this Youtube

u/ear7hin r/rust • Mentions

On “What are some good resource to better understand lifetime specifier?”

I'd recommend the "Crust of Rust: Subtyping and Variance" video. The main subject of subtyping is intimidating and tbh I don't think I absorbed much the first time I watched it, but working through a tricky example that requires lifetime annotations was helpful for me. link to this video

u/kinoshitajonain r/rust • Mentions

On “Problem with lifetimes in generics”

Subtyping and variance! huzzah! Let me first point you to a great video on the subject: link to this video Watch it from start to finish. It'll help you understand a lot about lifetime subtyping in Rust.

u/ExasperatedLadybugin r/typescript • Mentions

On “Just found out that you can't trust the TS transpiler with union types. A more complicated version of this made it to production and robbed me of my sanity...”

Here's a video that covers this topic in rust in great detail link to this video

u/Unusual-Pollution-69in r/rust • Mentions

On “What are some good resource to better understand lifetime specifier?”

This is pure gold, way better than the dedicated lifetime episode! After watching this I would really like the compiler to say to me: *The T in that's under mutable borrow here is invariant, and cannot be subtyped with anything shorter than 'static.* Or something like that. Because current *'x' does not live long enough* doesn't really make sense in here link to this video
1View on Reddit

u/ondrejdanekin r/rust • Mentions

On “Hey Rustaceans! Got a question? Ask here! (43/2022)!”

link to this video

u/vortexofdoomin r/rust • Mentions

On “How to make these lifetimes compile”

This does a great job at explaining why.

u/jamincanin r/rust • Mentions

On “What's the difference between `FnOnce`, `Fn` and `FnMut`”

/u/jonhoo made a video on variance that covers done of these differences in detail and that you might find helpful. link to this video