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"
for node in self.children
→for node in &self.children
. Also usetry!()
around eachwrite
. (welcome to format this into an answer.) – Carlie