Categories
Tags
algorithms APIT Arc arm assembly asynchronous base64 BitHacks Blogging box c clang-format client cmake compiler concat concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP dtruss Dynamic Example FFI flat_map format FP fsanitize Functional functions futures Fuwari GATs gccrs generics gitignore glibc GUI hacking hashmap haskell heap interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS Markdown math ML mmap nc OnceLock optimization OS panic parallels perf physics pin postgresql radare2 release reverse RPIT rust sanitizer science Science serialization server shift sin SmallProjects socket std strace String StringView strip strlen surrealdb SWAR swisstable synchronous tan toml traits triangulation UnsafeRust utf16 utf8 Video wsl x86_64 xilem zig
109 words
1 minutes
260107_traits03_debug_display
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/