Skip to main content

Element

Type Alias Element 

Source
pub type Element<'a, T, F> = NodeOrToken<Node<'a, T, F>, Token<'a, T, F>>;
Expand description

A node or a token in a tree.

This is what Tree::first, Node::first, Children, Walk and the other mixed iterators yield. It has the navigation methods of a Node, answering as an empty leaf when it is a token, so code like tree.first()?.children() works without first checking what the element is. Use into_node or into_token for what only one of them has, such as Node::id or Token::id.

§Examples

let tree = syntree::tree! {
    "root" => {
        ("lit", 3),
        "child" => {
            ("ws", 1)
        }
    }
};

let root = tree.first().ok_or("missing root")?;
let values = root.children().map(|e| e.value()).collect::<Vec<_>>();
assert_eq!(values, ["lit", "child"]);

let lit = root.first().ok_or("missing lit")?;
assert!(lit.is_token());
assert_eq!(lit.children().count(), 0);

let child = lit.next().and_then(|e| e.into_node()).ok_or("missing child")?;
assert_eq!(tree.get(child.id()), Some(child));

Aliased Type§

pub enum Element<'a, T, F> {
    Node(Node<'a, T, F>),
    Token(Token<'a, T, F>),
}

Variants§

§

Node(Node<'a, T, F>)

An interior node.

§

Token(Token<'a, T, F>)

A token.