Debug Trait का उपयोग
Debug trait एक struct को सिखाता है कि जब आप सिर्फ इसके अंदर झांकने की कोशिश कर रहे हों तो खुद को एक readable तरीके से कैसे print करें।#[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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Pretty-Printing with {:#?}
Alternate format specifier {:#?} एक Debug value को इंडेंटेशन के साथ कई lines में print करता है, कई fields वाले structs के लिए इसे पढ़ना आसान बनाते हुए।
उदाहरण: Pretty-Printing with {:#?}
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Debug on Nested Structs
Debug recursively derive होता है: अगर कोई struct field के रूप में दूसरा struct रखता है, वह inner struct भी पूरी चीज़ printable होने के लिए Debug derive करना चाहिए।
उदाहरण: Debug on Nested Structs
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Debug vs Display
Debug ({:?}) developer-facing diagnostic output के लिए है और auto-derive किया जा सकता है, जबकि Display ({}) polished, user-facing text के लिए है और इसे manually implement किया जाना चाहिए।
उदाहरण: Debug vs Display
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी struct को
{}से print करने की कोशिश करना जब यह सिर्फDebugimplement करता है,Displayनहीं -- ये अलग-अलग placeholder syntax उपयोग करते हैं। - किसी struct के ऊपर
#[derive(Debug)]जोड़ना भूल जाना, फिर एक compile error पाना कि typeDebugimplement नहीं करता। - Nicely indented, multi-line struct output के लिए pretty-print placeholder
{:#?}के बारे में न जानना।
#[derive(Debug)]किसी struct के लिए अपने आपDebugtrait implement करता है।{:?}वह format placeholder है जोDebugimplement करने वाली value print करने के लिए उपयोग होता है।{:#?}उसी output का एक nicely indented, multi-line pretty-print version produce करता है।Debugoutput अपना code debug कर रहे developers के लिए है, polished user-facing text के लिए नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: