Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use collections on the stack in Rust?

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.

like image 850
Andrew Wagner Avatar asked Dec 16 '14 13:12

Andrew Wagner


People also ask

Does rust have stacks?

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.

What is a collection rust?

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.

What is a rust data structure?

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.


1 Answers

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.

like image 87
Matthieu M. Avatar answered Sep 20 '22 04:09

Matthieu M.