How to implement Debug for dyn Trait

How do I implement std::fmt::Debug for a custom trait?

I want to be able to print Frame in my below example.

However, I get the compile error that (dyn Series + 'static)` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug

I am unsure how I should implement this for Series

trait Series {
    fn len(&self) -> usize;

struct Floats {
    data: Vec<f64>,
    name: String,

impl Series for Floats {
    fn len(&self) -> usize {

struct Frame {
    columns: Vec<Box<dyn Series>>,

fn main() {
    let f = Floats{data: vec![1.,2.],name: "H1".to_string()};
    let df = Frame{
        columns: vec![Box::new(f)],
    println!("{:?}", df);

Thanks for your help!


dyn Trait is the type on its own, and, if Trait is local to your crate (not imported), you can simply implement any trait (which does not require Self: Sized, of course) directly on trait object:

use core::fmt::Debug;
impl Debug for dyn Series {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Series{{{}}}", self.len())



thanks for your help. I didn‘t realize I could do this on dyn Series and was attempting to implement it on Floats which didn‘t help.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.