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

I am wondering if this can be compiled.

impl<T: fmt::Debug> fmt::Debug for 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.rowsis an iterator that gives &[T].

The error here is that Sized is not implemented for [T]in the context b.entry(e), which is bizarre because the iterator yields &[T], as mentioned earlier.

I cannot understand this, in part because I cannot understand what functions are signed here.

fn entry(self, entry: &Debug) -> DebugList<'a, 'b>

Pay attention to &Debug.

However, in the corresponding example documentation, the links are passed to the &i32builder.

struct Foo(Vec<i32>);

impl fmt::Debug for Foo {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.iter().fold(fmt.debug_list(), |b, e| b.entry(e)).finish()
    }
}

There should be something interesting to study with this great confusion.

The desired result will look like [[1, 2], [3, 4]].

A similar example that anyone 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()
}
+4
1

entry() :

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

fmt::Debug. , &[T], &fmt::Debug. , , , - . , - ; - &&[T], &fmt::Debug, &[T]. b.entry(&e) b.entry(e).

builder ; fold .

:

impl<T: fmt::Debug> fmt::Debug for Array2<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.rows().fold(&mut f.debug_list(), |b, e| b.entry(&e)).finish()
    }
}
+5
source

All Articles