petrbel
petrbel

Reputation: 2538

How to use generic VecDeque?

Basically I need to make a structure that contains a VecDeque of States. My code so far:

type State = [[bool]];
pub struct MyStruct {
    queue: VecDeque<State>,
}
impl MyStruct {...}

When compiling this code I end with

error: the trait `core::marker::Sized` is not implemented for the type `[[bool]]` [E0277]
note: `[[bool]]` does not have a constant size known at compile-time

I suppose that having State in the queue isn't good idea at all, so I tried a queue of references (which also fits into my application).

type State = [[bool]];
pub struct MyStruct {
    queue: VecDeque<&State>,
}
impl MyStruct {...}

In this case, even more weird error occures:

error: missing lifetime specifier [E0106]

How to create such a structure in order to work the way I wrote above? I'm really not a Rust expert.

Upvotes: 0

Views: 687

Answers (1)

DK.
DK.

Reputation: 59105

The basic problem is that [[bool]] makes no sense. [bool] is dynamically sized, and you can't have an array of dynamically sized values, so [[bool]] is just impossible.

It's not entirely clear what you're trying to accomplish here. The most obvious solution would be to just use Vec instead:

pub struct MyStruct {
    queue: VecDeque<Vec<Vec<bool>>>,
}

As for your "even more weird error", that suggests to me that you haven't read the Rust Book, specifically the chapter on Lifetimes. In order to write a structure containing borrowed pointers, you have to specify lifetimes.

Upvotes: 5

Related Questions