从枚举内的一个框中提取属性

huangapple go评论67阅读模式
英文:

Extracting an attribute from a Box inside an enum

问题

#[derive(Clone, Debug)]
pub struct Node<T> {
    value: T,
    left: Addr<T>,
    right: Addr<T>,
}

#[derive(Clone, Debug)]
pub enum Addr<T> {
    Addr(Box<Node<T>>),
    None,
}

impl<T> Addr<T> {
    pub fn is_none(&self) -> bool {
        matches!(&*self, Addr::None)
    }
}

impl<T> Node<T> {
    fn node_insert(mut n: Addr<T>, val: T) {
        let nd = Box::new(Node {
            value: val,
            left: Addr::None,
            right: Addr::None,
        });

        if n.is_none() {
            n = Addr::Addr(nd);
        } else {
            // You should access the left attribute like this:
            if let Addr::Addr(node) = n {
                // Access the left attribute of the Node struct
                node.left = node_insert(node.left, val);
            }
        }
    }
}

node_insert 函数中,要访问 Node 结构的 left 属性,您应该像上面的示例代码中那样进行访问,使用 if let 来将 n 解构为 Addr::Addr(node),然后您可以访问 nodeleft 属性并调用 node_insert 递归。

英文:
#[derive(Clone, Debug)]
pub struct Node&lt;T&gt; {
    value: T,
    left: Addr&lt;T&gt;,
    right: Addr&lt;T&gt;,
}

#[derive(Clone, Debug)]
pub enum Addr&lt;T&gt; {
    Addr(Box&lt;Node&lt;T&gt;&gt;),
    None,
}

impl&lt;T&gt; Addr&lt;T&gt; {
    pub fn is_none(&amp;self) -&gt; bool {
        matches!(&amp;*self, Addr::None)
    }
}
impl&lt;T&gt; Node&lt;T&gt; {
    fn node_insert(mut n: Addr&lt;T&gt;, val: T) {
        let nd = Box::new(Node {
            value: val,
            left: Addr::None,
            right: Addr::None,
        });

        if n.is_none() {
            n = Addr::Addr(nd);
        } else {
            node_insert(n.left, val);
        }
    }
}

I'm trying to implement a simple binary tree, and in the node_insert function I can't figure out how to access the left attribute of the Node struct when calling node_insert recursively.

Nothing I've done worked and I couldn't find any answer to help with it, probably because its somehow specific.

答案1

得分: 0

使用模式匹配

fn node_insert(mut n: Addr<T>, val: T) {
    match n {
        Addr::None => {
            n = Addr::Addr(Box::new(Node {
                value: val,
                left: Addr::None,
                right: Addr::None,
            }))
        }
        Addr::Addr(n) => Self::node_insert(n.left, val),
    }
}

然而,编译器正确地发出警告:

warning: value assigned to `n` is never read
  --> src/lib.rs:23:13
   |
23 |             n = Addr::Addr(Box::new(Node {
   |             ^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

由于您拥有 n,您正在写入一个局部变量而不是树。相反,您应该获取 &mut Addr

fn node_insert(n: &mut Addr<T>, val: T) {
    match n {
        Addr::None => {
            *n = Addr::Addr(Box::new(Node {
                value: val,
                left: Addr::None,
                right: Addr::None,
            }))
        }
        Addr::Addr(n) => Self::node_insert(&mut n.left, val),
    }
}
英文:

Use pattern matching:

fn node_insert(mut n: Addr&lt;T&gt;, val: T) {
    match n {
        Addr::None =&gt; {
            n = Addr::Addr(Box::new(Node {
                value: val,
                left: Addr::None,
                right: Addr::None,
            }))
        }
        Addr::Addr(n) =&gt; Self::node_insert(n.left, val),
    }
}

However, the compiler rightfully warns:

warning: value assigned to `n` is never read
  --&gt; src/lib.rs:23:13
   |
23 |             n = Addr::Addr(Box::new(Node {
   |             ^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

Since you took ownership of n, you're writing to a local variable and not to the tree. Instead, you should take &amp;mut Addr:

fn node_insert(n: &amp;mut Addr&lt;T&gt;, val: T) {
    match n {
        Addr::None =&gt; {
            *n = Addr::Addr(Box::new(Node {
                value: val,
                left: Addr::None,
                right: Addr::None,
            }))
        }
        Addr::Addr(n) =&gt; Self::node_insert(&amp;mut n.left, val),
    }
}

huangapple
  • 本文由 发表于 2023年2月26日 20:43:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定