std-method
Custom Debug with debug_struct
Construct diagnostic output using formatter builders. Include only fields safe to expose; the alternate flag can request multiline formatting. Expected stdout (not executed): Account { id: 2, token: "REDACTED" } Common mistake: Writing secrets into a derived or custom Debug implementation.
Formal Syntax & Structure
rust syntaxrust
f.debug_struct(name).field(name, &value).finish()Working Example
Example Snippetrust
fn main() { use std::fmt; struct Account { id:u32, token:String } impl fmt::Debug for Account { fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result { f.debug_struct("Account").field("id", &self.id).field("token", &"REDACTED").finish() } } let a=Account{id:2,token:"example".into()}; assert!(!a.token.is_empty()); println!("{a:?}");}