Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Option Enum" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen live codes map and is_some in Rust on the "option" type to demonstrate Rust's ability to specify additional behaviors by writing functions inside enums. A student's question regarding if impl is a Java interface is also answered in this segment.


Transcript from the "Option Enum" Lesson

>> I'm gonna do something really quick, cuz I do need to kind of speed this section up a little bit. I don't know, I always underestimate how much time things take. But I'm gonna implement the option enough in a small fashion right now. So we can kinda see that there's nothing actually magical happening.

And you can do this yourself in rust. And so I'm even try to do something that I didn't practice, just to see what happens, right? So we're gonna go like this, enum Option2 and it's gonna take in a generic. And I'm simply gonna have a None. There's a reason why you always put your empty value up the top.

There's like this special memory optimization that when you read too many lists, you can learn about it. But when you're learning a language, don't care about the optimizations. You're just trying to learn the language, then learn the environment then get good at using the language in a speedy practical sense.

And then we'll have Sone, which will take a T, there we go. All right, so we have this very beautiful option right here. Now here's one thing that makes rust super awesome, is that I can use the keyword impl and then I can actually impl on the option2.

Now this is pretty great, hold on the impl, needs to have a generic and the generic exists right there. So how you read this is effectively there's an implementation that takes in a generic and the generic rest on the type. And so look at this, that means I can actually add functions to my enum.

Now this is like my favorite part about rust and it also exists in go as well but you can't do with enums, is that functions are added after you declare the type. And this is kinda different versus classes in JavaScript. You define your class once, and that's what it is.

So if you need to add additional behavior, you're either doing inheritance or making your class bigger. Whereas with rust you don't do it that way, it's it's different. I have now specified my type, now I'm specifying my functions. And this doesn't sound like it's the greatest thing ever, but when we start working with strings or iterators and think like that I actually becomes super cool because you can start specifying this additional behavior on top of the of your class.

It reminds me lot the goal of react with user effect use state know this, is that you gonna attach behaviors to your component, right? In reality, it's kind of like here's my class, and then here's all my extra properties coming in afterwards or my extra methods of how I get these properties.

So it's very composable. And so this allows what I feel like is a bit more composability to your types, that's something like TypeScript just doesn't have or Java if you're more familiar with that. I don't know about C sharp, C sharp is a magical language I haven't seen in a long time.

So let's go like this I'm gonna create the exact same function that we had before which is, how about this one, is some. Now that will tell us is this option does it have a value cuz in regular land if I go like this let foo equals some 5, I can go if foo is some, right?

Is it a value, then I know for a fact that it's safe to call unwrap. Now usually it's not the best way to do this, you can obviously pattern match the value out as opposed to doing it this way. But there you go, you also have this option.

So I can create the exact same thing on mine, where what I need to return out is a Boolean. so what I'll do is I'll return a match of myself, right? This just means this function takes a reference to myself or have the type option. I'm gonna take a reference to myself, and I'm going to match on it.

And if I am a None, I'll return false. And if I am a some of doesn't matter of anything, I return burn true, all right? Mitch match types, no. What is it? Okay, so that means I believe it do this. I have to do this or there's always these things you got to play around with.

My goodness right here right now, this expression is this. Found this, found emum option T type. We can just do it in a more simple fashion, right? We can do this in a more simple fashion. I can go if let's, I know exactly why, I'm such a fool.

Option2, right, remember cuz we've specified our own. Using the same words sometimes causes your brain to make silly mistakes, there we go anyways. So now my special some2, that right here, I now have the exact same thing. Look at that, it works just the same. This is effectively all options are underneath the hood is something just like this, or they just simply have some sort of enum that looks like this.

And then they have a bunch of implementations hanging off it. So you can kind of visualize what it is. Now, is it exactly this underneath the hood, probably not. But can you guess what it looks like? Yeah, pretty simple. And if we go like this, some, I can take this and jump to what it looks like.

Obviously it has a bunch of like compile time stuff added on top of it. But as you can see, it's just an enum, takes type T, none first cuz remember the memory optimization some T. So it's like literally the same thing that we just built right there. And I bet you if you look around you're gonna find a very similar looking item right here.

And is some, let's see if we can find it. Look at that, I don't even know what this thing means. It has some sort of sweet macro that does this and there we go. And so it's almost the exact same thing. Now you can guess what matches does right?

Matches just simply take self and is it a some with some? It just does a pattern match on it, right? Okay, easy, we could actually probably even write that if I could write macros fast. But that's sweet, right, none of that should be too hard, right? Let's see I mean I'm just gonna put a new main there so we don't have any errors.

So there we go, we just implemented something that looks like the option inside of rust. That's pretty cool, right? It was super cool being able to attach methods. We're gonna do the exact same concept here. You guys can program along and we'll do it. I'm going to not program this, yeah.

>> Yeah, so is it impl basically on Java interface?
>> Let's see so, info would be more like so to write this equivalent thing in say TypeScript if this wasn't say this, right? Because we can't do enums with types. Impl is a lot the same as doing class Option2 with a generic on it and then actually having public is some and actually having the concrete value in here.

Impl is just the implementation of these functions. And if you want the outside world to be able to see it outside of your file, you had the word pub to it cuz that means that makes it public. Obviously, It'd be really silly to have a public function on something that's not public itself.

So if somebody else wanted to use yours and import it in, you'd have to specify it as public. But there you go, exact same concept. Results are here you go, here's all this. We've kind of went over all that. So pretty radical stuff. We've already kind of done some questions.

We're gonna keep on moving on.

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