5

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"
Community
  • 1
  • 1
XAMPPRocky
  • 3,160
  • 5
  • 25
  • 45
  • 1
    `for node in self.children` → `for node in &self.children`. Also use `try!()` around each `write`. (welcome to format this into an answer.) – bluss Jun 27 '15 at 01:50

2 Answers2

5

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

oli_obk
  • 28,729
  • 6
  • 82
  • 98
4

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);
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155