Is there a way to use the collections from Rust's standard library on the stack? Assume you know in advance an upper bound on the number of items you will want to store in the collection.
Stack in RustA stack is a collection of items that can be added to and removed from, but only in the following order: last in - first out (LIFO). For example, if you board an overcrowded train, you will be the first to need to exit for other passengers to do so as well.
Rust's standard library includes a number of very useful data structures called collections. A collection is something that holds zero or more elements in some fashion that allows you to enumerate those elements, add or remove elements, find them and so forth.
Rust uses the collections library to support and implement several common data structures. A collection refers to a collection of one or more values stored in the heap memory. This means that the collection size does not need to be known before compilation.
If you look at the implementation of Vec
(admittedly one of the simplest collections), you will notice:
use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
Unlike C++, the collections are not yet parameterized by an allocator. It is something that is certainly desirable, in the long term, however this was not deemed necessary for 1.0.
If I remember the discussions correctly, some proposals hinged on HKT (Higher Kinded Types) for example, which are not a thing yet.
So, for now, no.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With