如何使用通用 VecDeque?

How to use generic VecDeque?

基本上我需要制作一个包含 VecDequeState 的结构。到目前为止我的代码:

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

编译这段代码时我以

结尾
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

我想在队列中加入 State 根本不是个好主意,所以我尝试了一个引用队列(它也适合我的应用程序)。

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

在这种情况下,出现了更奇怪的错误:

error: missing lifetime specifier [E0106]

如何创建这样的结构才能按照我上面写的方式工作?我真的不是 Rust 专家。

基本问题是[[bool]] 没有意义[bool]dynamically sized,并且您不能拥有动态大小值的数组,因此 [[bool]] 是不可能的。

不完全清楚您要在这里完成什么。最明显的解决方案是只使用 Vec 代替:

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

至于您的 "even more weird error",我认为您还没有阅读 Rust Book, specifically the chapter on Lifetimes。为了编写包含借用指针的结构,您必须指定生命周期。