Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively print struct in `fmt::Display`

I'm currently in the process of implementing fmt::Display for a struct so that it will print out to the console. However The struct has a field which is a Vec of it's type.

Struct

pub struct Node<'a> {
    pub start_tag: &'a str,
    pub end_tag: &'a str,
    pub content: String,
    pub children: Vec<Node<'a>>,
}

Current fmt::Display (invalid)

impl<'a> fmt::Display for Node<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "START TAG: {:?}", self.start_tag);
        write!(f, "CONTENT: {:?}", self.content);
        for node in self.children {
            write!(f, "CHILDREN:\n\t {:?}", node);
        }
        write!(f, "END TAG: {:?}", self.end_tag);
    }
}

Desired Output

START TAG: "Hello"
CONTENT: ""
CHILDREN:
   PRINTS CHILDREN WITH INDENT
END TAG: "World"
like image 256
XAMPPRocky Avatar asked Jun 27 '15 01:06

XAMPPRocky


2 Answers

There is a (somewhat hidden) feature of Debug, you can use the format specifier {:#?} to pretty-print your object (with indents and multiple lines). If you rewrite your struct's elements to have the same order as your requested output and derive the Debug trait

#[derive(Debug)]
pub struct Node<'a> {
    pub start_tag: &'a str,
    pub content: String,
    pub children: Vec<Node<'a>>,
    pub end_tag: &'a str,
}

then your output can look like this:

Node {
    start_tag: "Hello",
    content: "",
    children: [
        Node {
            start_tag: "Foo",
            content: "",
            children: [],
            end_tag: "Bar"
        }
    ],
    end_tag: "World"
}

Try it out in the PlayPen

like image 80
oli_obk Avatar answered Nov 03 '22 16:11

oli_obk


It seems you are confusing Display and Debug.

{:?} uses the Debug trait for formatting. You probably didn't implement Debug on your type, which is why you'd get an error. To use the Display trait, write {} in your format string.

write!(f, "CHILDREN:\n\t {}", node);
like image 41
Francis Gagné Avatar answered Nov 03 '22 18:11

Francis Gagné