Array of VecDeque in a struct

How do I make an Array of VecDeque in a struct? I specifically want an array because I want to have the array built on the stack (not the heap like a Vec would do). The Array can be a bunch of pointers to the VecDeque if necessary. However I'm not entirely sure how this works?

struct Order {}

struct OrderBook {
    price_levels: [VecDeque<Order>; 1000]

impl OrderBook {
    pub fn new () -> OrderBook {
        OrderBook {
              price_levels: [  ???? ; 1000] <-- not sure what to put here to make it compile?

Arrays in rust hard to use and that's why we always recommend using a Vec. If you must use an array, you will have to repeat VecDeque::new() one thousand times, use unsafe, or use a crate that uses unsafe.

Note that a large array is likely to overflow the stack.

Initialization of non-Copy array requires code repetition (macro) or an unsafe code in the current version of Rust. See for the current best possible way.

Why, though? If you are using VecDeque, you'll be allocating dynamically anyway.

1 Like

Good point.

I was planning on building something like this:

That's the winning implementation of an orderbook in a contest for speed. Instead of an array of vectors they use an array of structs pointing to the first and last order in a linked list and a max number of orders is generated on the stack so that no new orders are added to the heap during execution.

I was planning to do something similar but for some reason switched it to a vecdeque after thinking that it would be faster to have my orders contiguous in memory. (completely forgot the point was to have things built on stack)...

Thinking about it now it seems like an array of structs is the way to go if I want to keep it on the stack. Thanks!