I want to implement the fmt::Display
for a nested struct commonly used in my code.
// The root structure
pub struct WhisperFile<'a> {
pub path: &'a str,
pub handle: RefCell<File>,
pub header: Header
}
pub struct Header{
pub metadata: metadata::Metadata,
pub archive_infos: Vec<archive_info::ArchiveInfo>
}
pub struct Metadata {
// SNIP
}
pub struct ArchiveInfo {
// SNIP
}
As you can see, this is a non-trivial tree of data. The archive_infos
property on Header
can be quite long when presented as one line.
I would like to emit something along the lines of
WhisperFile ({PATH})
Metadata
...
ArchiveInfo (0)
...
ArchiveInfo (N)
...
But when I try to display Vec<ArchiveInfo>
I get that Display is not implemented. I can implement fmt::Display
for ArchiveInfo
but that's not enough since fmt::Display
is not implemented for the parent container Vec
. If I implement fmt::Display for collections::vec::Vec<ArchiveInfo>
I get the impl does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types
.
I have tried iterating over the vec and calling write!()
but I couldn't figure out what the control flow should look like. I think write!()
needs to be return value of the function but that breaks down with multiple calls.
How can I pretty print a Vec of my structures?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…