How to implement a custom ‘fmt::Debug’ trait?

According to the example from the std::fmt docs:

extern crate uuid;

use uuid::Uuid;
use std::fmt;

struct BlahLF {
    id: Uuid,
}

impl fmt::Debug for BlahLF {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Hi: {}", self.id)
    }
}

The part to emphasize is the fmt:: in fmt::Result. Without that you’re referring to the plain Result type. The plain Result type does have two generic type parameters, fmt::Result has none.

Leave a Comment