简体   繁体   English

用 Box 初始化结构<none>价值</none>

[英]Initialize struct with Box<None> value

I am trying to make a flashcard program in rust, that will be saved in json format.我正在尝试在 rust 中制作一个抽认卡程序,它将以 json 格式保存。 I have made this struct for each card:我为每张卡片制作了这个结构:

#[derive(Serialize, Deserialize)]
struct Card{
    question:String,
    answer:   String,
    status:    Status,
    strength:    f32,
    stability:    f32,
    difficulty:    f32,
    review_history: Vec<Review>,
    children:      Vec<Card>,
    parent:       Box<Option<Card>>
}

In this app a card may be related to another card in a tree-structure, so tahts why theres a parent variable and a children vector.在这个应用程序中,一张卡片可能与树结构中的另一张卡片相关,所以这就是为什么会有一个父变量和一个子向量的原因。 I learned that if a struct refers to itself it has to be in a Box or something similar, and in my case, not every card has a parent, as it might be a root, so thats why i also included Option.我了解到,如果一个结构引用它自己,它必须在一个 Box 或类似的东西中,在我的情况下,并不是每张卡都有父卡,因为它可能是一个根,所以这就是为什么我还包括选项。 Im not getting errors on this.我没有收到错误。

However when I try to initialize a root-card like this i get an error:但是,当我尝试像这样初始化根卡时,出现错误:

let newcard = Card{
                question: question, 
                answer: answer, 
                status: Status::Normal,
                strength: 1f32,
                stability: 1f32,
                difficulty: 0.5f32,
                review_history: review_history,
                children: children,
                parent: Box<None>,

                   };

Error i get is: "comparison operators cannot be chained" on the parent: Box<None> line.我得到的错误是:父级上的“比较运算符不能被链接”: Box<None> 行。 it suggests using Box::<None> but then i get the error "Constructor is not visible due to private fields".它建议使用 Box::<None> 但随后我收到错误“由于私有字段,构造函数不可见”。

Im not sure which fields are private, my struct, box, or the option enum?我不确定哪些字段是私有的、我的结构、框或选项枚举?

Also Im wondering why the compiler doesnt complain about Vec<Card> not being in a box, since I just learned you use box to avoid recursive structs on the stack我还想知道为什么编译器不抱怨 Vec<Card> 不在一个盒子里,因为我刚刚了解到你使用盒子来避免堆栈上的递归结构

Just use Box::new to initialize the value:只需使用Box::new来初始化值:

Box::new(None)

Playground 操场

Also Im wondering why the compiler doesnt complain about Vec not being in a box, since I just learned you use box to avoid recursive structs on the stack我还想知道为什么编译器不抱怨 Vec 不在一个盒子里,因为我刚刚了解到你使用盒子来避免堆栈上的递归结构

Compiler do not complain about Vec<Card> because it makes no sense to complain about it.编译器不会抱怨Vec<Card>因为抱怨它是没有意义的。 Vec allocation are heap, only a pointer the len and capacity are stack based. Vec 分配是堆的,只有 len 和容量的指针是基于堆栈的。

You can check the the information in the guarantees part of the documentation :您可以查看文档保证部分中的信息

A vector containing the elements 'a' and 'b' with capacity 4 can be visualized as below.包含容量为 4 的元素“a”和“b”的向量可以如下所示进行可视化。 The top part is the Vec struct, it contains a pointer to the head of the allocation in the heap, length and capacity.最上面的部分是 Vec 结构,它包含一个指向堆中分配头部的指针,长度和容量。 The bottom part is the allocation on the heap, a contiguous memory block.底部是堆上的分配,一个连续的 memory 块。

            ptr      len  capacity
       +--------+--------+--------+
       | 0x0123 |      2 |      4 |
       +--------+--------+--------+
            |
            v
Heap   +--------+--------+--------+--------+
       |    'a' |    'b' | uninit | uninit |
       +--------+--------+--------+--------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM