Skip to content
0:00 / 0:00

#rust#option#nicheoptimization

Choose the Right Option

Logan Smith

51K subscribers • 6 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • A classic. People kept recommending it from 2023 to 2026.
  • Presented by Logan Smith

Intermediate. Assumes working knowledge.

119K views3 years ago2 recommendations

Programming languagesSystems programmingRustMemory managementAPI design

Logan Smith explains the memory layout and API design trade-offs between Option<&T> and &Option<T> in Rust.

"Reference-to-Option" and "Optional-reference"* are both ways to represent "a reference to a value that may or may not be there" in Rust. So which one should you use in your APIs? Hear me spoil my answer within the first 10 seconds, and then stick around to get real nerdy about a topic you may have never spent 15+ minutes thinking about before. Featuring special guest appearances from memory layout visualizations (feat. niche optimizations), the venerable ToOwned trait, and me saying the word "Data" several hundred times. And yes, I do in fact regret missing the opportunity to make a Sum 41 reference during the memory layout visualization. Option docs - doc.rust-lang.org/std/option/enum.Option.html ToOwned docs - doc.rust-lang.org/std/borrow/trait.ToOwned.html Niche optimization / "null pointer optimization" (which IS guaranteed for Option-ref-T) - doc.rust-lang.org/std/option/index.html#representation I use the amazing Manim library for animating these videos, and I edit them with Blender and Audacity. manim.community/ blender.org/ audacityteam.org/ *YouTube doesn't allow angle brackets in video descriptions.

Why it's here

2 people recommend this

What people say. Recommended as a great intermediate explanation of Rust's Option type memory layout and API design choices.

u/ERROR_23in r/rust • Recommends

On “Methods or free functions”

Pretty much every argument in this video applies to the &Vec<T> vs &T. In fact the author himself said it in another video.

u/TrainShot2642in r/rust • Recommends

On “I just want to say I love Rust”

There are some good intermediate channels, like this [video But for beginners-beginners there is nothing better than just reading The Book and doing rustlings