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

The "Collections 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 add a field to is_costal:bool, return a City described as non-coastal based on a boolean, call a new city, print the description of rustville using string interpolation.

Preview
Close

Transcript from the "Collections Exercise" Lesson

[00:00:00]
>> So same structure as before, here we've got main.rs. This time we just have some different code in there and a different set of to dues. So here we've got our struct called city. City has 2 fields right now, a description, which is a string and residents which is a use 64.

[00:00:15]
Our first to do is we wanna add a field here called is_coastal, which is a Boolean. This will cause some other compiler errors, but those other compiler errors will tell you what other changes need to happen. So this may be your first experience of encountering rusts, compiler helping you out and helping you figure out like what needs to happen.

[00:00:34]
So let's take a look at our new city function. So basically this is saying, give me some residents and a Boolean for whether or not it's coastal. And if it's coastal, I'm going to return a city struck, which has this description. A coastal city will have approximately this many residents, so we're using format to do string interpolation here.

[00:00:51]
Residency may recall as shorthand for residents:residents. But we're just writing it in that sort of shorthand way. Otherwise, we're gonna panic and say, to do return a city as described as a non-coastal city of approximately, blah, blah, blah residence. So we're gonna do something pretty similar to this first branch.

[00:01:08]
Notice, by the way, that here, this is an expression and also this whole if it's an expression. So basically this function is using an implicit return here. Without actually having to write the return keyword so it's returning the result of this if, and then the result of this if in each branch is this expression right here, so we're sort of combining the two things we saw previously.

[00:01:29]
And then down here we have our main function. Which is actually what's kicking things off, previously in the previous example we had new non main function defined after main, here we're defining it before. Either way is totally fine. You can feel free to move it around if you prefer.

[00:01:44]
And what we've got is we have rustville, which is a city and it's saying the rustville is being assigned to a panic. So as to call a new city here with whatever arguments you like. Now because this is a panic, we can see that the ID is actually grayed out this print line like I did on my slide earlier.

[00:02:00]
Saying, hey, if you hover over it says, unreachable statement and the reason it's unreachable is because it's going to have panicked by then. So as soon as your fix this to do, this will no longer be unreachable. And then finally it's saying if rustville that is coastal which is an error because we haven't defined that yet.

[00:02:17]
Then it's gonna print it is a coastal city, otherwise it will print it is not a coastal city. So sum up, we have three to do's, first return a city here, second called new_city here. And then finally, print the description of rustville here just using string interpolation, like before.

[00:02:33]
String interpolation plus accessing a fuel off of a struct

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