Check out a free preview of the full The Rust Programming Language course
The "Pattern Matching Exercise" 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:
Students are instructed to handle the other CitySize variants individually and use City::new() to create a Metropolis-sized city.
Transcript from the "Pattern Matching Exercise" Lesson
[00:00:00]
>> All right, as with all the other exercises, we have main.rs and this time, we're doing something a little bit different. We've got in addition to our city struct from last time where we have description residents and his coastal, now we also have an enum called cities sizes.
[00:00:14]
We got these little comments here that are sort of documenting about what each of these sizes correspond to. So a town is about 1000 people, a city it's about 10,000, Metropolis is about a million. These values will come up a little bit later in the exercise. So this time, you may recall in our previous exercise, we had a function called New_city.
[00:00:33]
Well, here we're doing it sort of the more idiomatic rust style where we have an impulse city and inside that we're saying function new insights. So we'll say city colon colon new rather than new_city when we're calling this function, but otherwise it's gonna be exactly the same as before.
[00:00:48]
By the way, you might notice this is returning a city, if you want, you could have a return self up to you. Okay, so this is still thinking city size is coastal. Now we're doing things a little bit differently here. So, we're doing a pattern match on this city size that we were given.
[00:01:03]
Remember city sizes either town, city or Metropolis and based on the city size, like the general description of the city size we're getting, we're going to make up a residence value and this is 1000 for a town and that's where this thing comes in. It'll be 10,000 for a city and a million for metropolis.
[00:01:17]
And then what we're actually doing is in each of these branches we're returning a tuple. So here I'm returning a tuple, where the first one is a string. That's that same description string we had last time and the second one is the the residence value. So then I'm saying let and then I'm doing a tuple D structure here, equals and then the whole match.
[00:01:36]
So basically what ends up happening is after we run this match, we end up with description in scope and residents in scope. Because each of the branches of this match is returning a tuple. And then what we do with that tuple, once we get it back is we just immediately D structure it and put it into scope.
[00:01:49]
So this is an example of a pretty common technique that you see in rust. It's sort of a way to quote and quote, return multiple values. We're not actually returning multiple values, we're returning a tuple. But a tuple can hold multiple values. So this is kind of a relatively lightweight trick for doing that.
[00:02:07]
So here's our first to do we wanna handle the other city size variants individually. Now similarly to how the town variant is handled here, so we wanna duplicate that one for city and Metropolis and change each of their residence counts as appropriate. Finally, at the end of all this once we've actually pulled out our description and residents Then we construct the cities with description residents and his coastal.
[00:02:28]
And that's the end of our city colon colon new method, okay? And then finally the last thing that we have to do here is inside main, we're basically going to use city colon colon new in order to create a metropolis sized city here. So we're gonna replace this sort of hard coded definition where we're building the struct by hand with a colon to the city colon colon new method.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops