debugging - Using Formatter::debug_list to implement Debug for a two-dimensional array -


i'm wondering if it's possible compile.

impl<t: fmt::debug> fmt::debug array2<t> {     fn fmt(&self, f: &mut fmt::formatter) -> fmt::result {         let ref mut builder = f.debug_list();         self.rows().fold(builder, |b, e| b.entry(e)).finish()     } } 

self.rows iterator yields &[t].

the error here sized not implement [t] in context of b.entry(e), bizarre because iterator yields &[t] mentioned before.

i'm not able figure out, in part because can't make sense of function signatures involved here.

fn entry(self, entry: &debug) -> debuglist<'a, 'b> 

note &debug.

yet relevant documentation example passing references &i32 builder.

struct foo(vec<i32>);  impl fmt::debug foo {     fn fmt(&self, fmt: &mut fmt::formatter) -> fmt::result {         self.0.iter().fold(fmt.debug_list(), |b, e| b.entry(e)).finish()     } } 

with confusion there has interesting learn.

the desired output [[1, 2], [3, 4]].

a similar example can compile:

use std::fmt;  fn fmt<t: fmt::debug>(vec: &vec<t>, f: &mut fmt::formatter) -> fmt::result {     let ref mut builder = f.debug_list();     vec.chunks(4).fold(builder, |b, e| b.entry(e)).finish() } 

entry() defined thus:

pub fn entry(&mut self, entry: &fmt::debug) -> &mut debuglist<'a, 'b>; 

it takes fmt::debug trait object. when pass &[t], wants cast implicitly &fmt::debug. this, however, cannot done, trait objects can constructed of sized objects. solution make trait object of sized slice; is, pass of type &&[t] can implicitly converted &fmt::debug, containing type &[t]. is, b.entry(&e) instead of b.entry(e).

your builder line unnecessary , introduces lifetime problems; should declare part of fold call convenience.

this leaves final result:

impl<t: fmt::debug> fmt::debug array2<t> {     fn fmt(&self, f: &mut fmt::formatter) -> fmt::result {         self.rows().fold(&mut f.debug_list(), |b, e| b.entry(&e)).finish()     } } 

Comments