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

The "Q&A" 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 answers questions about the differences between Option and Result enums.


Transcript from the "Q&A" Lesson

>> Why have result and option? Why can't we just have one? Well, let's think about the definition really quickly. An option has a value associated with it, right? It has a sum that has type V, whatever your type is, and it has none, there's nothing there. Technically, it's specified this way.

There's a reason optimization. I forget what it's even called, but either way, this is what it looks like. But where's the result enum? It has two types associated, the value and the error. So that means you'd always have an error of error none type if there was nothing there, there's just a lot more you'd have to specify.

I'm sure there's even more reasons that I don't even know. But to me, it just seems like it would be a lot of extra steps to understand the difference. So that's kind of effectively the two different types right here, it's just they don't technically have an overlap. They only have an overlap on the positive side.

The negative side, one is nothing. The other one is a completely different type. And so that is why we have two different types. I mean, in some sense, it would be pretty convenient to be able to have one type. I'm sure there's some things that would make it nice.

But for me, I like this. It separates out the behaviors in my head. Because ultimately, errors can happen where undefined aren't, and undefined can happen where errors aren't. All right, we got one more question?
>> Can we use enums as a result types?
>> You want an enum inside of a result?

Yeah, so the thing is that completely composable. So if I had an enum color right, we have that from earlier, and I have yellow and whatever, I can also come in here and I can define a result type with a color subtype, so you can have a color in here.

Remember, it's just a generic. So that means I could have a vector of color as what comes out, it's just the error simply wraps whether it's positive or negative. The inner type is the inner type you specify, so that inner type can be whatever. You can have a result of option, right, why can't you have that?

Of course, you can have that. I could have a result of option of enum if I wanted to, meaning the thing could error and the thing, if it doesn't error, could return nothing. So it kinda tells you what's going on. So you can walk through what the service does by simply looking at its type.

Pretty neat, makes you feel pretty good? Makes me feel pretty good. I like it, I like it, makes me feel so safe. All right, we got one more. Let's do one more.
>> Could you use filter here instead of the if statement?
>> Well, the problem is, as I said, you have to print out line not a number.

Filter will simply remove that item. So yeah, if you didn't wanna do that, so here's a cool thing you could do, I'll just do it really quickly. For whoever said that, I'll get rid of all of this, and I'll just simply print right here. So there is this concept called a filter map, filter map.

What it does is it takes in an option or it takes in the value, and you produce out an option.
>> So what that means is that I would have the following. I would have, here's a line, and I can go line.parse, and I'll tell it to use size.

Now, right now, this gets out, remember, this gets out a result. You can convert a result to an option where the error is turns into none, and the value turns into sum. So I could just go, okay? And what this is gonna, I forgot to actually call it as a function.

What this will do is it will actually, for every positively parsed item, it will map that and send it through. For every failed parse, it will just simply be filtered out and ignored forever. And so now, we just go line and remember, if you remember our numbers. We have foo right in the middle, and so when we execute this, foo's gone.

Where did foo go? Well, it didn't parse correctly, and so it got turned into none, none got filtered. Boom, we're great to go. All right, there you go, so you could do that. So again, combinators are pretty cool, allow for some pretty snazzy behavior.

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