Check out a free preview of the full The Rust Programming Language course

The "Pattern Matching Recap and Q&A" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard provides a brief review of enums, pattern matching, methods, type parameters and answers student questions. Student questions regarding what would happen with duplicate matches using pattern matching, if default values can be set in pattern matching, and if there is a way to add additional methods after an impl has been created are covered in this segment.

Preview
Close

Transcript from the "Pattern Matching Recap and Q&A" Lesson

[00:00:00]
>> Cool so to review Part 3, we talked about enums first. So here's an example of making a custom enum called purple, so this is a custom color. We talked about how you need to have this sort of namespace prefix before your enums. Which is true for almost all enums, except for results in option, which are baked into the standard library.

[00:00:19]
Talks about how to do pattern matching. So I could say match on a particular color value. And then I could enumerate all the different possibilities. Or if I wanted to, I could leave some of them off, but then I wouldn't be able to use it as an expression.

[00:00:31]
Whenever I'm using it as an expression because it needs to be able to know, what to assign that value 2, I do have to enumerate all the different possibilities. And if I don't wanna enumerate all of them, I can just put an underscore at the end, and then it'll fall back into that default case.

[00:00:45]
But here I'm pattern matching on Color::Custom. And if it happens to be a custom variant, then I am extracting and using de structuring syntax. To pull out this tuple of r, g and b, and then I can just use those inside my branch here, like I would in any normal conditional expression.

[00:01:03]
We'll start about methods, so here's a how I could call color dot rgb, and method style, where things are now changeable, it's going to be using that self inside the ample. Or I could use it in traditional style where I just use :::rgb, which is basically just saying, I want to make use of that input namespace for this method.

[00:01:23]
Finally we talked about type parameters. So here's an example of using option. So in this case I'm using option sharp but, because option is defined with a T type parameter or a type parameter named T. It's possible for me to put any type that I want inside of those angle brackets and have it still work.

[00:01:40]
>> Might have missed this for matching multiple times on a variant. How does it decide which one, to pattern match, like, any shows in an example.
>> So like if we had green in here multiple times or something like that, okay, well, I guess I can give a thorough answer.

[00:01:57]
So, if I were to put green in here multiple times, I would get a compiler error, saying that, the second pattern is unreachable. It might actually just be a warning but certainly, if I had green in here multiple times, only the first one would actually get matched. Any ones below that would not get matched, because it goes through and tries to match each of these in order.

[00:02:14]
But, something that can happen and this is a little bit advanced for this course is, in some cases, you can do stuff like, :::custom, and then rather than just saying red here, I could say like. Red;0, meaning I only want you to match this branch if red is 0, and if red is specifically 0, then I want to run this logic and then de structure, green and blue as normal.

[00:02:33]
And then in that case, I could have another ::custom variant, below this that had like red, green and blue, defined like this, in which case, it would be all the variants where red is not 0. So, those are basically all the different scenarios you could have where, duplication either would or wouldn't make sense.

[00:02:51]
>> Is it possible to set default values for those, like saying like red equals 0 or red equals to 255 or something like that?
>> Is it possible to set default values for this. So when you're pattern matching, this is really about asking the question of like what's actually in there.

[00:03:08]
So, a default value wouldn't really work in the sense that it's not really possible for it to be missing. If you do have custom then like, red, green and blue have to be there. And if you have yellow or green, there is no such thing as red, green and blue.

[00:03:21]
So if you wanted to, I guess you could define like default values in these branches, but yeah, this one it wouldn't really do anything.
>> I believe you said, there's no prototyping in Rust, correct?
>> Yeah, so Rust does not have, like prototypes in the sense that like JavaScript does also doesn't have classes or inheritance.

[00:03:41]
>> So if you were to ever define an enum, you made a variable that uses a node as a check. You wouldn't be able to add extra functionality like a custom method, after you've already initiated it, have that available.
>> Yeah, so question is like once you make an enum, you define an info on it or for structure that matter.

[00:04:03]
There's no way to add additional methods on to it after the fact. There actually is, which that's basically what you use traits for. It doesn't do it with inheritance, but there is, a way to do that, [LAUGH] essentially.

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