← Back to context

Comment by tialaramex

4 hours ago

For "adding an element to a vector" it's actually not necessarily what you meant here and in some contexts it makes sense to be explicit about whether to allocate.

Rust's Vec::push may grow the Vec, so it has amortized O(1) and might allocate.

However Vec::push_within_capacity never grows the Vec, it is unamortized O(1) and never allocates. If our value wouldn't fit we get the value back instead.