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

The "Collections 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 Collections exercise.

Preview
Close

Transcript from the "Collections Solution" Lesson

[00:00:00]
>> So first we're gonna add a field here for is coastal which is going to be a bool, is coastal. And this will cause some other compiler errors. Namely, we see that city is now underlined. So it says hey missing structure fields is coastal. Yes, absolutely. Okay well we need to specify that so I'm gonna say is coastal and then I could say is coastal is said to be equal to the is coastal variable in here but of course if I want to I can also just leave it off like that because that's typically how it's done.

[00:00:35]
All right, so now we've got our new city function is returning the is coastal value that's passed in here. We also want this else branch to do kind of the same thing but, instead of saying a coastal city it's gonna say a non coastal city of approximately same number of residents, everything else is gonna be duplicated.

[00:00:53]
So there's probably some way we could make this, even less code duplication, but for the purpose of this exercise, that was the main goal is just to make a note of these city things and just return it. Okay, so now our new city function is done, we've gotten rid of those two to dos and our next to do is we want to call new city here with whatever arguments you like.

[00:01:16]
So I'm going to call instead of panic I'm going to say new city and it takes residence which is a U64. I'm going to give it I don't know, 123 residents and a is it coastal? I'll say no, it's not. Okay, so that's no longer an error. And here, we're just gonna say this city can be described as print its description here and too many.

[00:01:39]
So I'm gonna say rustville.description. And I can do that because rustville is a city struct, and it's just allowed to do that. Now notice, by the way, that we have this residents field here, which is never read, like we never actually use this residence thing, we set it but we never read it anywhere.

[00:02:01]
So this is just the rust compiler helping us out a little bit and basically giving us a little bit of feedback that, hey, since you've defined this field on your struct, but you're never actually using it, maybe you want to get rid of it. And that's not necessarily a given that you do, maybe you do or do not want to do that.

[00:02:17]
But it is an example of the compiler sort of helping you out and letting you know that that's something you might want to be aware of. So let's run the program. Okay, and now you can see we still got this warning field has never read residents but that's okay.

[00:02:34]
Not a big deal. And now we can see this city can be described as a non coastal city of approximately 123 residents which is coming from there.

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