Check out a free preview of the full The Rust Programming Language course
The "Vectors 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 use .pop() to remove the last city, use .push() to move last_city back into city_names, and print each of the city names.
Transcript from the "Vectors Exercise" Lesson
[00:00:00]
>> All right the format will be familiar this point. So, we've got main and some to dues. This time, we don't have any other functions, it's just all inside main, and basically, we're going to be working with Vex for the first time. So, we've got city names, which is our vac here, and it's got Python Nia Java's, Berg Sea by the Sea, and rust Velez as a the names of our cities.
[00:00:20]
And we're gonna say last city here and we're gonna use the pop method on the veck to remove the last city from the list. As a hint, you might remember that we learned in the previous section, that pop actually returns an option because what if the veck is empty.
[00:00:34]
It needs to be able to return something so option is gonna return sorry option is gonna contain either some or none depending on whether or not the value is there, and as a tip, I put the syntax for that right here. Then, we're gonna get that last city I'm going to say last city dot starts with R.
[00:00:52]
And if it does start with an R, then we're gonna print line, this thing starts with R. Otherwise we're gonna print that it doesn't start with an R. Next to do is say okay, now that we're done printing whether or not it starts with R, which was really important, we really needed to do that, like immediately.
[00:01:05]
Now we're going to use dot push to put that last city back into city names. And then finally we're going to print the entire list of cities by iterating over the the elements of the veck and printing them out one at a time. So this is going to be our first for loop that we're writing from scratch.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops