109 words
1 minutes
260107_traits03_debug_display
2026-01-07

Debug 구현하기#

  • derive macro를 써서 손 쉽게 구현
#[derive(Debug)]
struct Young {
    data: String,
}

fn main() {
    let my_str = Young {
        data: "test".to_string(),
    };
    println!("Debug {:?}", my_str);
}  
use std::fmt;

struct Young {
    data: String,
}

impl fmt::Debug for Young {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.data)
    }
}

fn main() {
    let my_str = Young {
        data: "test".to_string(),
    };
    println!("Debug {:?}", my_str);
}

Debug과 Display 구현은 똑같다.#

  • {:?} Debug
  • {} Display
260107_traits03_debug_display
https://younghakim7.github.io/blog/posts/260107_traits03_debug_display/
Author
YoungHa
Published at
2026-01-07