printf - Implement fmt::Display for Vec<T> -
i want implement fmt::display
nested struct commonly used in code.
// 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 can see, non-trivial tree of data. archive_infos
property on header
can quite long when presented 1 line.
i emit along lines of
whisperfile ({path}) metadata ... archiveinfo (0) ... archiveinfo (n) ...
but when try display vec<archiveinfo>
display not implemented. can implement fmt::display
archiveinfo
that's not enough since fmt::display
not implemented parent container vec
. if implement fmt::display collections::vec::vec<archiveinfo>
the impl not reference types defined in crate; traits defined in current crate can implemented arbitrary types
.
i have tried iterating on vec , calling write!()
couldn't figure out control flow should like. think write!()
needs return value of function breaks down multiple calls.
how can pretty print vec of structures?
as error states, cannot implement trait type don't own:
the impl not reference types defined in crate; traits defined in current crate can implemented arbitrary types
however, can implement display
wrapper type. piece missing use try!
macro:
use std::fmt; struct foo(vec<u8>); impl fmt::display foo { fn fmt(&self, f: &mut fmt::formatter) -> fmt::result { try!(write!(f, "values:\n")); v in &self.0 { try!(write!(f, "\t{}", v)); } ok(()) } } fn main() { let f = foo(vec![42]); println!("{}", f); }
Comments
Post a Comment