简体   繁体   English

如何制作树状结构,其中节点是 Rust 中的特征对象?

[英]How can I make tree-like structure where the nodes are trait objects in Rust?

I have a rust-playground link here , and the full code for my basic example is:我在这里有一个 rust-playground 链接,我的基本示例的完整代码是:

trait Obj {
    fn hit(&self, t0: f64, t1: f64) -> bool;
}

struct Leaf {
    data: f64,
}

impl Obj for Leaf {
    fn hit(&self, t0: f64, t1: f64) -> bool { return t0 < self.data && self.data < t1 }
}

struct Node<'a> {
    data: f64,
    left: &'a Box<dyn Obj>,
    right: &'a Box<dyn Obj>,
}

impl<'a> Obj for Node<'a> {
    fn hit(&self, t0: f64, t1: f64) -> bool {
        self.left.hit(t0, t1) || self.right.hit(t0, t1)
    }
}

impl<'a> Node<'a> {
    fn new(tmin: f64, tmax: f64) -> Self {
        let mid = (tmin + tmax) / 2.;
        if tmax - tmin < 1. {
            return Self { data: tmin, left: &Box::new(Leaf {data: tmin }), right: &Box::new(Leaf {data :tmax} ) };
        }
        let left = Node::new(tmin, mid);
        let right = Node::new(mid, tmax);
        Self {data: tmin, left: &Box::new(left), right: &Box::new(right) }
    }
}

fn main() {
    let node = Node::new(0., 100.);
    ()
}

It's a contrived example, but it gives the same compiler errors I get in my real version.这是一个人为的例子,但它给出了我在真实版本中遇到的相同编译器错误。 In the constructor for Node, I get the following errors:在 Node 的构造函数中,出现以下错误:

error[E0308]: mismatched types
  --> src/main.rs:29:83
   |
29 |             return Self { data: tmin, left: &Box::new(Leaf {data: tmin }), right: &Box::new(Leaf {data :tmax} ) };
   |                                                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Obj`, found struct `Leaf`
   |
   = note: expected reference `&'a std::boxed::Box<(dyn Obj + 'static)>`
              found reference `&std::boxed::Box<Leaf>`

error[E0308]: mismatched types
  --> src/main.rs:33:33
   |
33 |         Self {data: tmin, left: &Box::new(left), right: &Box::new(right) }
   |                                 ^^^^^^^^^^^^^^^ expected trait object `dyn Obj`, found struct `Node`
   |
   = note: expected reference `&'a std::boxed::Box<(dyn Obj + 'static)>`
              found reference `&std::boxed::Box<Node<'_>>`

Why do I get these errors?为什么我会收到这些错误? Both Leaf and Node implement Obj , and my understanding is that they could both be of type dyn Obj . LeafNode都实现了Obj ,我的理解是它们都可以是dyn Obj类型。 I think it has something to do with lifetimes, but I can't think of how to fix this.认为它与生命有关,但我想不出如何解决这个问题。 What's the proper way of making this binary tree structure in Rust?在 Rust 中制作这种二叉树结构的正确方法是什么?

Box<..> is an owned type, so it can live on its own, been moved without a lifetime track. Box<..>是一个拥有的类型,因此它可以独立存在,在没有生命周期的情况下被移动。 In your example you simply can remove all of the references and a lifetime, so the struct definition becomes:在您的示例中,您只需删除所有引用和生命周期,因此结构定义变为:

struct Node {
    data: f64,
    left: Box<dyn Obj>,
    right: Box<dyn Obj>,
}

Here a full example. 这里有一个完整的例子。

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

相关问题 树状结构中的递归 - Recursion in a Tree-like Structure 从树状结构构建哈希 - Build hash from tree-like structure 如何使用php将树状数组输出到列表中? - How do I output an tree-like array into a list with php? 如何使用连续传递样式遍历树状结构时实现尾调用优化 - How to acheive tail call optimization while traversing tree-like structure using continuation-passing style 在树状结构中查找具有属性的第一个对象 - Find first occurrence of an object with property in tree-like structure 将数据帧编码为树状嵌套 if 结构的有效方法? - Efficient way to encode a dataframe into a tree-like, nested-if structure? 树状结构中的成本/数量汇总-Python - Cost/Number Rollup In Tree-like structure - Python 如何从平面文件(Gene Ontology OBO文件)生成递归的树状字典? - How do I generate a recursive tree-like dictionary from a flat file (Gene Ontology OBO file)? 将属性递归地添加到树状结构中的每个节点,并返回修改后的树 - Recursively add property to every node in a tree-like structure and return modified tree 如何根据xlsx工作表构建树状数据 - How to build tree-like data according to a xlsx work sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM