First of all, Rust does not offer (in the standard library) any library with a guaranteed delay for adding elements: Rust collections can usually allocate memory when new elements are added, and allocating memory can take an unlimited amount of time in the worst case.
Moreover, for each case there are two applicants:
- the stack can be implemented either on top of
Vec
or LinkedList
(both pop_back
and push_back
functions) - the queue can be implemented either on top of
VecDeque
or LinkedList
(both pop_front
and push_back
functions)
The difference between Vec*
and LinkedList
is that the latter is simplified: memory is allocated for each push_back
call. On the one hand, this is great, because it means that the cost of push_back
does not depend on the number of elements that are already in the collection, on the other hand ... well, memory allocation can take a very long time.
The first is a bit more complicated:
- it has better throughput due to more convenient caching
- it has extra capacity, ensuring that
push_back
is not push_back
while there is excess capacity. - it still retains the depreciation of O (1)
push_back
, even if you do not reserve excess power ahead of time
In general, I would recommend using Vec
for the stack and VecDeque
for the queue.
source share