如何在安全Rust中表达相互递归的数据结构?

我正在尝试在Rust中实现类似于场景图的数据结构。我想要一个等效于用安全 Rust 表示的C ++代码:


struct Node

{

    Node* parent; // should be mutable, and nullable (no parent)

    std::vector<Node*> child;


    virtual ~Node() 

    { 

        for(auto it = child.begin(); it != child.end(); ++it)

        {

            delete *it;

        }

    }


    void addNode(Node* newNode)

    {

        if (newNode->parent)

        {

            newNode->parent.child.erase(newNode->parent.child.find(newNode));

        }

        newNode->parent = this;

        child.push_back(newNode);

    }

}

我想要的属性:


父母对子女拥有所有权

可以通过某种引用从外部访问节点

碰到一个事件Node可能会改变整个树


富国沪深
浏览 906回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP