Rfcs: Proposal for last_val() for Vectors

Created on 7 Aug 2020  ·  3Comments  ·  Source: rust-lang/rfcs

Hi,
you have for Vec's the methods last() and last_mut() but if you want to get the value itself you have to use pop() and it breaks the semantic. I would propose to introduce last_val() that could be a simple alias to pop() since a moved value cannot exist two times, so it has to be deleted from the list. But last_val() would confuse fewer users searching for something that returns the last value instead of a reference.

Most helpful comment

In pretty much all languages, “extracting the last value of a container” is called pop. I don’t see the basis of the statement that users would be less confused with your suggestion.

All 3 comments

In pretty much all languages, “extracting the last value of a container” is called pop. I don’t see the basis of the statement that users would be less confused with your suggestion.

This is one of those changes that realistically would have had to happen more than 5 years ago to be worth it. Even assuming (without taking a position on it) that last_val is a slightly better name than pop, that's not worth the churn to switch a common method nor worth the added confusion of two methods that do the same thing.

The description of pop in the docs says

Removes the last element from a vector and returns it

so hopefully this is still sufficiently discoverable for anyone looking to perform this operation.

When I just looked at the issue title, I was expecting this to be a moving projection from Vec<T> to T instead of &mut Vec<T> to T. The name last_val itself is alone confusing enough.

Was this page helpful?
0 / 5 - 0 ratings