How to use fmt::Display with an iterator?

Hello everyone,

This is rather simple but I can’t wrap my head around it. I’m implementing the Rubik’s Cube like this:

// Each Cubie has a set of xyz coordinates
#[derive(Debug)] 
struct Cubie {
    xyz: [i8; 3],
    xyz_color: [Color; 3], // Color is an enum, it implements #[derive(Debug)]
}
#[derive(Debug)]
struct Cube {
    cubies: Vec<Cubie>,
}

And I want to display it like this:

impl fmt::Display for Cube {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for cubie in self.cubies.iter() {
            write!(
                f,
                "{}{}{}{:?}{:?}{:?}\n",
                cubie.xyz[0],
                cubie.xyz[1],
                cubie.xyz[2],
                cubie.xyz_color[0],
                cubie.xyz_color[1],
                cubie.xyz_color[2],
            )
        }
    }
}

But rustc tells me:

| |______^ expected enum `std::result::Result`, found ()

Is this because I’m iterating over a vector? What can I do?

you need ?; after the write! call

EDIT and an Ok(()) at the end

1 Like

Thank you very much, I’m still having trouble with this Result<(), E> syntax.
Have a nice day!

1 Like

It’s awesome once you get used to it :slight_smile: