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.
pub struct Node<'a> {
pub start_tag: &'a str,
pub end_tag: &'a str,
pub content: String,
pub children: Vec<Node<'a>>,
}
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);
}
}
START TAG: "Hello"
CONTENT: ""
CHILDREN:
PRINTS CHILDREN WITH INDENT
END TAG: "World"
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With