How to derive Debug for struct with `&'a dyn Trait`?

trait A {}

impl std::fmt::Debug for dyn A {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         todo!()
    }
}

#[derive(Debug)]
pub struct B<'a> {
    pub values: &'a dyn A,
}

fn main() {}

this is not compiling, and I do not have a good idea on how to address it.

Playground: Rust Playground

1 Like

Some options:

1 Like

Thanks!

I was missing the lifetime identifier, the below is enough:

trait A {}

impl std::fmt::Debug for dyn A + '_ {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         todo!()
    }
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.