Check out a free preview of the full Rust for TypeScript Developers course

The "Options in TypeScript vs Rust" Lesson is part of the full, Rust for TypeScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen explains Option types are a useful way to handle null and undefined values. The Some and None variants explicitly define the type and whether or not the option handles null or undefined values.

Preview
Close

Transcript from the "Options in TypeScript vs Rust" Lesson

[00:00:00]
>> So we're gonna actually talk about options now, because options are enums. But they're the answer to null and undefined at the exact same time, which means you can just create your own option type. And you can have your own way you want to handle none versus something there in Rust.

[00:00:17]
It's pretty neat, but let's go over a quick challenge. Is this last line valid? Why or why not? Yeah, no one can really answer that question effectively cuz it's a crazy question, isn't it? So answer, of course, is that depends on what you have enabled inside your TSConfig.

[00:00:36]
If you have exact optional properties, that means this is illegal. Because technically, if you called object.keys, you'd actually have a bar. But you've said either there's nothing or it's a string. You didn't say it's undefined and string, you said it's nothing or string. So, again, TypeScript, a lot of loosey goosey behavior.

[00:00:56]
Sometimes it's wonky or weird to kind of reason about things. Just kind of like little fun fact in here. So Rust obviously handles them different. Here is my TypeScript, or here's my Rust definition of an Option. Notice it's generic, that means it can take any type you want it to be.

[00:01:12]
And it's gonna be None, meaning there's nothing there. Or it's gonna be Some with value T, or type T underneath. So just like how we did custom, usize, and string, Option does no type, or whatever type you define, they're generic. So that's why when I do Some five, underneath the hood, it's Option of type usize.

[00:01:36]
But when I noticed when I did None, it said, hey, you need to type, annotate me cuz I have no idea what you are. I can redo that really quickly. So if I go let foo = Some(5), I'm just creating an enum of Option. That is in i32.

[00:01:51]
But if I go let foo = None, it's gonna go, I don't know what you are. What are you? Cuz you haven't defined your generic yet. So pretty neat. That's where you're gonna have to do something like, maybe I'm an Option or String. Why not, I can be whatever I want to be.

[00:02:07]
I just might not be there. So that's what Option means, so you have None and Some. And obviously, as you can see, when I did green stuff, or when I did Item, you'll notice that I had to do these little colons right here. Rust just provides it so that you can just call Some, instead of Option::Some.

[00:02:22]
It's the same thing. That's what's actually happening underneath the hood right now. You're just calling Some, or you're calling None. All right, so Options, they're very, very important. But why? Why do we need Options in Rust? Well, it always comes down to memory. So when JavaScript land, when you are returning a value, it actually creates a box value that is controlled by the garbage collector.

[00:02:44]
There's also sneeze, which are handled slightly differently, but nonetheless, there's something that has to hold on to your value. And so it's boxed, meaning we don't know what it is, but it's a box with a type discriminator that's pointing somewhere else to the actual value that you have.

[00:03:00]
So Rust does the same thing. It just does it with Option. You have a box that boxes Option, and it has one or two values. And so going back here, you can imagine we would drop this and we'd have this being the None case, and this being the Some case.

[00:03:17]
And so that is what's happening underneath the hood. So when Rust returns an Option, underneath the hood, on the stack, it can create that memory. And so that way, we don't have any sort of weird things happen. We'll go over a little bit more of what happens on the stack, but that's why it has to exist.

[00:03:32]
There's no such thing as nothing in a strict type language, because it has to create the exact memory size that it needs to put something in it. And if it just sometimes is there, sometimes not, well, it just would never work out. It'd be too crazy inside your system.

[00:03:46]
Things would just be going nuts. Since they are enums, you can use if/match statements. You can do all the same tricks we've already learned. If let some x equals the option. So I can jump back here and go, if Some(x) = foo. Now remember, the last time I just defined foo, it's an option String.

[00:04:04]
So if you look at this, oops, if let, you'll notice it's type String. I just lifted out the value. Identical stuff, you've already used these, you've already seen them, this is an enum.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now