Open balt-dev opened 1 day ago
I'm willing to make a PR with this if everyone's okay with these changes as-is.
Note: It might be a good idea to have first_or_push
and friends as well.
I think having a Vec::push
equivalent that returns a reference to the just-pushed value is independently useful, maybe name it Vec::push_and_get
?
At that point, why not have Vec::push
just return &mut T
(barring any concerns over the breaking change)?
This is what #464 was about, but I closed it in favor of this because I don't want to focus on two ACPs at once. I might reopen it after this one is settled if it's independently useful like you say, but I'd need a better example for it.
Proposal
Problem statement
Getting a reference to the value you just pushed to a vector requires an extra call and unwrap:
Motivating examples or use cases
I'm currently porting https://github.com/Naruyoko/OmegaNum.js/ to Rust, and this is a common pattern all over:
x.array[i+1]=(x.array[i+1]||0)+1;
This is directly translated asWith this, this could be the following:
which is much nicer.
Solution sketch
Add functions for
Vec::last_or_push(T) -> &T
,Vec::last_mut_or_push(T) -> &mut T
,Vec::last_or_push_with(impl FnOnce() -> T) -> &T
,Vec::last_mut_or_push_with(impl FnOnce() -> T) -> &mut T
, which would get the last element of the vector, or push a value and return a reference to that.Alternatives
Alternatively,
Vec::push
could return&mut T
, which would remove the possiblity of a panic - maybe it would be better, but both would be nice. See issue #464.