The Rust Programming Language

Pattern Matching Solution

Richard Feldman

Richard Feldman

Vendr, Inc.
The Rust Programming Language

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

The "Pattern Matching Solution" 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 live codes the solution to the Pattern Matching exercise. Student questions regarding how to create a custom resident count and how to encode values rather than types in the enum varience are also covered in this segment.

Preview
Close

Transcript from the "Pattern Matching Solution" Lesson

[00:00:00]
>> Okay, so to recap, we have this city size thing and the town is 1000 residents, the city is 10,000 the metropolis is 1 million. So when we are duplicating this branch, I'm just gonna copy paste this whole thing and just change those numbers around, basically. So, we've got town, what to do city and also metropolis.

[00:00:24]
City has a residence of 10, 000s and Metropolis was a million. And so here I'm just gonna replace the word here. I'm gonna say, a city of approximately that many residents and here a metropolis. Again, as before, if we wanted to, we could do some more code sharing here.

[00:00:39]
But since this is just an exercise, we're not gonna bother with that. The main thing to note is that we now have three branches. And all three of them are basically the three different variants of this enough. We're not using the payloads in this example. But if we want to, we could very well introduce a payload here and just say like, hey.

[00:00:55]
Metropolis now has an i64 inside of it, but we're not gonna bother with that for this case. Okay, we're still getting this error here. And we're gonna fix that by replacing this city struct with a call to city colon new. And it takes two arguments, one is a city size, so I'm gonna pick metropolis.

[00:01:16]
Why not? And notice, my autocomplete here now actually has the three possible variants here. So this is something that rust analyzers aware of it knows well, there's exactly three different possibilities that could be here. So just pick metropolis and then as in the previous one this time, I guess I will make it be coastal.

[00:01:33]
Why not? So we got a coastal metropolis, as many of them are I suppose. Okay, and now I should be able to run this and once again we're gonna get some warnings. We'll come back to those in a second, but says this city is a metropolis of approximately a million residents.

[00:01:51]
Wow. Okay, now we did see is coastal is never read here, so that was a warning there for this thing. Because in this example we never actually read the coastal field recall in the previous example that we never read the residents field. Nothing to be concerned about, solely fine for things to be unused.

[00:02:08]
But you might also notice that we see this warning variant is never constructed city very it is never constructed town. So actually the warning that we're getting here is it saying hey, you said these two are possibilities but in practice, you never actually use either of these two.

[00:02:22]
And that's because I chose metropolis down here. Now if I change this to city now it's gonna complain the town of Metropolis are not used and city was used. So this is another interesting feature of enums is that it's a way of expressing here are all the different possibilities, but Russ will say.

[00:02:38]
Hey, if some of these possibilities never come up in practice, maybe you might as well just get rid of them. Not so that you have to deal with, and there are ways you can suppress those warnings if you want to. But worth noting that one of the nice things about enums, is that the compiler is really keenly aware of these are all the possibilities that you have to deal with.

[00:02:56]
There are no other possible values that we could have for city size other than the exact three that you defined here.
>> I was curious if you could illustrate how we could build like a custom example. Say it wasn't it didn't fall in either of those three, and we just wanted to enter our own resident amount

[00:03:13]
>> Yeah, sure. Okay, so I'll add another one, okay. So I can say area with, with residents, you 64. I could do it either like this. So this would be the struct style or if I wanted to, I could put it in parentheses like this. Now it's kinda interesting to see, whoops, I do in parentheses and I can't label it.

[00:03:31]
So I'm going to use it struct style just because otherwise it's not really clear what that u64 is doing. So here it's a little bit clear that's like, okay, it's the residence count. So notice that when I do this, I'm now going to get an error here. On mass city sites where it says missing match arm.

[00:03:48]
Nonexhaustive can't move my mouse to show you. But nonexhaustive patterns area dot, dot, dot, not covered. Ensure that all possible cases are being handled, possibly by adding wild cards or more match arms. So basically what it's telling me is like hey you need to either. Do something like this where I make this be an underscore that will fix that problem, and it won't complain about that anymore.

[00:04:11]
Or I need to add an explicit match arm for our new area thing and I'll say residents okay, and now I guess I'm just gonna not bother doing that. I'm just gonna. [LAUGH] We've already got the resident that was stored in there. And so now I'll just do that, that's how we do it.

[00:04:33]
And everything else should work, although it's gonna complain, the area's never constructed, but I guess we want to see that in action. I could just say, city size area and then I'd say residents I don't know this is a really small area five people in this one. Okay, and yeah, sure enough it runs the city is a, I said metropolis in there.

[00:04:52]
How about an area, there we go. We run that, so yeah, this city is an area of approximately five residents. So there you go.
>> Is there a way to encode values rather than types in the enum variants if I wanted to, instead of those comments, I could sort of say that town is 1000 there and there's 10,000 or is that a terrible idea?

[00:05:15]
And then I could, so I don't have to define what the numbers are in a particular implementation method or you're just saying.
>> Yeah, so the question is like, is there some way to encode these in the types so that we don't have to put it in a comment?

[00:05:29]
Unfortunately, not types, well, okay, I guess it's like it's really ridiculously overcomplicated. Type shenanigans you could engage in to try and do that, but I wouldn't recommend it. I think the short answer is like I pretty much no, I think a comment would be the way to go for something like this.

[00:05:46]
Or of course you could have a hard coded constant, but yeah.

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