It is annoying to manually fetch the last element, which is unsurprisingly quite common after a push. The most obvious thing would be to make Vec::<T>::push() return a &mut T, but this is strictly speaking a breaking change. It would be cool to get some crater data on whether this in practice is an issue (i.e. if people don't put the ; after push() in functions returning ()).
The more realistic change is to add a method, push_ref, returning &mut T.
Hi @ticki,
Why not push_mut_ref according to conventions?
I'd really like this too, but bikeshed: I don't like the name push_ref; it sounds like you're pushing a reference. Maybe pushed_ref?
@KalitaAlexey Two reasons:
The actual idiom is to use push_mut (like AsMut::as_mut).
Using _mut makes no sense when there is no non-mutuable alternative.
@comex what about pushed()? It's simpler and nicer IMO.
@ticki Sure.
The emplacement implementation could be made to return the reference to an element that has been just emplaced. I wonder, though, which is more useful – returning the index of the element or the reference to the element itself.
@nagisa
Returning a reference allows working around the implicit boundcheck, and honestly, getting the index isn't that hard (it's free too).
IIRC, v.back() <- elem was supposed to produce a &mut T to the element.
Most helpful comment
IIRC,
v.back() <- elemwas supposed to produce a&mut Tto the element.