打印树 — 尝试访问字段,但未找到具有该名称的字段

Printing a tree — attempted access of field, but no field with that name was found

我正在尝试编写我的第一个 Rust 程序。我想在屏幕上打印一棵简单的树,但我无法访问 value 属性,它说

Error 1 attempted access of field value on type Node, but no field with that name was found c:\users\zhukovskiy\documents\visual studio 2013\Projects\rust_application1\rust_application1\src\main.rs 21 20 rust_application1

use std::io;

enum Node {
    Branch { value: i32, next: *const Node },
    Leaf { value: i32 }
}

fn main() {
    let leaf = Node::Leaf { value: 15 };
    let branch = Node::Branch { value: 10, next: &leaf };
    let root = Node::Branch { value: 50, next: &branch };

    let current = root;
    loop {
        match current {
            Node::Branch => { println!("{}", current.value); current = current.next; },
            Node::Leaf => { println!("{}", current.value); break; }, 
        }
    }
}

只是因为 Node 的两个变体都有一个 value 字段,并不意味着您可以直接访问它。您可以通过匹配值来获得它(这些是等价的):

let value = match leaf {
    Node::Branch { value, .. } => value,
    Node::Leaf { value } => value,
};

let value = match leaf {
    Node::Branch { value, .. } | Node::Leaf { value } => value,
};

但如果您要经常这样做,您可能需要添加一个方法:

impl Node {
    pub fn get_value(&self) -> i32 {
        match self {
            &Node::Branch { value, .. } => value,
            &Node::Leaf { value } => value,
        }
    }
}

...然后您可以像这样使用它:

let value = leaf.get_value();

利用我的直觉魔力,我猜你有这样的代码:

enum Node {
    Branch { value: i32 },
    Leaf { value: i32 },
}

fn main() {
    let leaf = Node::Leaf { value: 15 };

    println!("{}", leaf.value);
}

其中确实有错误:

<anon>:9:20: 9:30 error: attempted access of field `value` on type `Node`, but no field with that name was found
<anon>:9     println!("{}", leaf.value);
                            ^~~~~~~~~~

问题是 leaf 的类型是 Node,而 Node 有两个变体,BranchLeaf。没有名为 Node::BranchNode::Leaf 的类型。您需要匹配枚举以详尽处理所有情况:

enum Node {
    Branch { value: i32 },
    Leaf { value: i32 },
}

fn main() {
    let leaf = Node::Leaf { value: 15 };

    match leaf {
        Node::Branch { value } => println!("Branch [{}]", value),
        Node::Leaf { value }   => println!("Leaf [{}]", value),
    }
}

由于您的所有枚举变体都具有相同的字段,您可以将该字段提取到外部结构中,并且只保留枚举内部不同的字段。这样您就可以直接访问内部 value 字段。当你想知道你的节点是 Branch 还是 Leaf 时,你需要在 kind 字段上进行匹配。此外,我建议使用 Rc<Node> 而不是 *const Node,因为访问 *const Node 指向的值需要不安全的代码,并且很可能会让您在更复杂的代码中遇到麻烦。

enum NodeKind {
    Branch(*const Node),
    Leaf,
}

use NodeKind::*;

struct Node {
    value: i32,
    kind: NodeKind,
}

fn main() {
    let leaf = Node{ value: 15, kind: Leaf };
    let branch = Node { value: 10, kind: Branch(&leaf) };
    let root = Node { value: 50, kind: Branch(&branch) };
}

我想你真正想要的是下面的代码:PlayPen