← Back to Rust Course | Chapter 7: Structs | Lesson 5 of 7

Debug Trait का उपयोग

Debug trait एक struct को सिखाता है कि जब आप सिर्फ इसके अंदर झांकने की कोशिश कर रहे हों तो खुद को एक readable तरीके से कैसे print करें।
Syntax
rust
#[derive(Debug)]
struct Name {
    field: Type,
}

println!("{:?}", value);
println!("{:#?}", value);

Deriving Debug

किसी struct definition के ऊपर #[derive(Debug)] जोड़ना अपने आप एक implementation generate करता है जो आपको debugging purposes के लिए struct की fields print करने देता है।

उदाहरण: Deriving Debug

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Point` to group related fields together
struct Point {
    x: i32,
    y: i32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p`, set to `Point { x: 3, y: 7 }`
    let p = Point { x: 3, y: 7 };
    // Print "{:?}" to the console, substituting in `p` (with a trailing newline)
    println!("{:?}", p);
}

Pretty-Printing with {:#?}

Alternate format specifier {:#?} एक Debug value को इंडेंटेशन के साथ कई lines में print करता है, कई fields वाले structs के लिए इसे पढ़ना आसान बनाते हुए।

उदाहरण: Pretty-Printing with {:#?}

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `User` to group related fields together
struct User {
    name: String,
    age: u32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `user`, set to `User { name: String::from("Grace"), age: 40 }`
    let user = User { name: String::from("Grace"), age: 40 };
    // Print "{:#?}" to the console, substituting in `user` (with a trailing newline)
    println!("{:#?}", user);
}

Debug on Nested Structs

Debug recursively derive होता है: अगर कोई struct field के रूप में दूसरा struct रखता है, वह inner struct भी पूरी चीज़ printable होने के लिए Debug derive करना चाहिए।

उदाहरण: Debug on Nested Structs

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Address` to group related fields together
struct Address {
    city: String,
}

// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Person` to group related fields together
struct Person {
    name: String,
    address: Address,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p`, set to `Person { name: String::from("Sam"), address: Address { city: String::from("Oslo") } }`
    let p = Person { name: String::from("Sam"), address: Address { city: String::from("Oslo") } };
    // Print "{:?}" to the console, substituting in `p` (with a trailing newline)
    println!("{:?}", p);
}

Debug vs Display

Debug ({:?}) developer-facing diagnostic output के लिए है और auto-derive किया जा सकता है, जबकि Display ({}) polished, user-facing text के लिए है और इसे manually implement किया जाना चाहिए।

उदाहरण: Debug vs Display

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Item` to group related fields together
struct Item {
    name: String,
    price: f64,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `item`, set to `Item { name: String::from("Widget"), price: 9.99 }`
    let item = Item { name: String::from("Widget"), price: 9.99 };
    // Print "Debug view: {:?}" to the console, substituting in `item` (with a trailing newline)
    println!("Debug view: {:?}", item);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी struct को {} से print करने की कोशिश करना जब यह सिर्फ Debug implement करता है, Display नहीं -- ये अलग-अलग placeholder syntax उपयोग करते हैं।
  2. किसी struct के ऊपर #[derive(Debug)] जोड़ना भूल जाना, फिर एक compile error पाना कि type Debug implement नहीं करता।
  3. Nicely indented, multi-line struct output के लिए pretty-print placeholder {:#?} के बारे में न जानना।
चैप्टर सारांश
  • #[derive(Debug)] किसी struct के लिए अपने आप Debug trait implement करता है।
  • {:?} वह format placeholder है जो Debug implement करने वाली value print करने के लिए उपयोग होता है।
  • {:#?} उसी output का एक nicely indented, multi-line pretty-print version produce करता है।
  • Debug output अपना code debug कर रहे developers के लिए है, polished user-facing text के लिए नहीं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.