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

The "Vectors 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 Vectors exercise. A student's question regarding why match is needed is also covered in this segment.


Transcript from the "Vectors Solution" Lesson

>> Great, so let's go through these two. So first we're gonna use pop to remove the last city from the list. So, here's an example of pattern matching syntax. So remember if I just say city, this is not gonna work the press escape. Okay, fixed Sorry, it's gonna wait for this to go away.

So if I call, that's not gonna work because we're gonna get an error here where it says hey, wait a minute, this is an option not as string. So here's how we can deal with that is I can actually just match on citynames.pop directly and just do one of these.

I can uncomment these. And now, I've got this nice little match right here, and I can just change this inner value. I'm just gonna return that right here first. In the case of none, I'm just gonna use empty string. Okay. So, there's one other problem here, which is that city names is not mutable and pop is something that actually mutates like it takes something off of the back.

So you can say cannot borrow city names as mutable. We haven't talked about borrowing yet, but it does at least have a suggestion at the end there consider changing this to be mutable, and that will in fact, fix the problem. So now this is no longer giving us an error, citynames.pop.

And now we're basically able to do the pop, get back our option and then we say either it's some in which case we wanna just record that city name the inner value, or we're just going to default to empty string and just say there was no city name that was just an empty string there instead.

Okay, next now we've got our last city which is a string we can call starts with and then so it's going to print either these things great. Now we've done that we're going to use dot push to put last city back in city name so we're going to say city names dot push, last city Oops.

Okay. And finally, we want to print out the full list of cities. So we're gonna do for city, we've got to see this, actually just copy paste this right here. So we're gonna say for city name in city names dot iter. And we'll just print each of these with a little asterisk next to them so sort of like a bullet point.

City name all right, everything compiles let's see how it runs. Cool Rossville starts with an R. I agree. Here's the full list of cities Python to Java's Burg c by the sea and westville. Any questions?
>> When once more could you explain why do we need the match again?

>> Sure so basically, the pop function or the pop method does not return a string even though this is a vector of strings. And the reason it doesn't return that is because Russ doesn't have a concept like null or undefined or nil. So what happens if this vector happens to be empty when I call pop on it, like what is it actually returned?

So because there's no possible value It could return. Will rust does is instead of returning something like nil or undefined or panicking, what it does instead is it returns this option enum, and the option basically represents the concept of the value that might or might not be there.

And so, if we have the subvariant, that means the value is there. In which case, we can pattern match on it and get it out. But, there also might be none which indicates there was not value there. And so, basically this branch will get run If we call pop and there is something to pop off there, but otherwise it's not gonna work.

It's just gonna come back as as none.
>> So, rust is forcing you to explicitly write your edge case scenarios into your code.
>> Yeah, it's as if you had, like null checking built into the language where it said like you If you get back no you have to handle the possibility that it's no except that rather than having it be no rust basically uses this concept that's already got of enums to accomplish something similar.

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