Display और Debug Traits
Display और Debug आपके type को खुद को readable text में बदलने के दो अलग तरीके सिखाते हैं: एक लोगों के लिए polished, एक developers के लिए detailed।In this page:
use std::fmt;
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.field)
}
}
#[derive(Debug)]
Deriving Debug
#[derive(Debug)] अपने आप एक Debug implementation generate करता है, एक struct को quick developer-facing inspection के लिए {:?} से 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: 1, y: 2 }`
let p = Point { x: 1, y: 2 };
// Print "{:?}" to the console, substituting in `p` (with a trailing newline)
println!("{:?}", p);
}
Login to try C/C++/Java/PHP code in the editor
Implementing Display Manually
Display में कोई automatic derive नहीं है और इसे हाथ से implement किया जाना चाहिए, बिल्कुल यह define करते हुए कि {} के ज़रिए clean, user-facing output में type कैसा दिखना चाहिए।
उदाहरण: Implementing Display Manually
// Bring `std::fmt` into scope
use std::fmt;
// Define a struct `Point` to group related fields together
struct Point {
x: i32,
y: i32,
}
// Implement methods on `fmt`
impl fmt::Display for Point {
// Define the function `fmt` taking `&self`, `f` and returning a `fmt::Result`
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `p`, set to `Point { x: 3, y: 4 }`
let p = Point { x: 3, y: 4 };
// Print "{}" to the console, substituting in `p` (with a trailing newline)
println!("{}", p);
}
Login to try C/C++/Java/PHP code in the editor
Implementing Both Traits on the Same Type
एक type एक साथ Display और Debug दोनों implement कर सकता है, इसे दो distinct textual representations देते हुए इस आधार पर कि {} या {:?} उपयोग हुआ।
उदाहरण: Implementing Both Traits on the Same Type
// Bring `std::fmt` into scope
use std::fmt;
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Money` to group related fields together
struct Money {
cents: i64,
}
// Implement methods on `fmt`
impl fmt::Display for Money {
// Define the function `fmt` taking `&self`, `f` and returning a `fmt::Result`
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "${:.2}", self.cents as f64 / 100.0)
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `price`, set to `Money { cents: 1999 }`
let price = Money { cents: 1999 };
// Print "Display: {}" to the console, substituting in `price` (with a trailing newline)
println!("Display: {}", price);
// Print "Debug: {:?}" to the console, substituting in `price` (with a trailing newline)
println!("Debug: {:?}", price);
}
Login to try C/C++/Java/PHP code in the editor
Using write! Inside fmt
write! macro वह तरीका है जिससे Display और Debug implementations दोनों अपना output produce करते हैं, formatted text को दिए Formatter में लिखते हुए और एक fmt::Result return करते हुए।
उदाहरण: Using write! Inside fmt
// Bring `std::fmt` into scope
use std::fmt;
// Define a struct `Temperature` to group related fields together
struct Temperature(f64);
// Implement methods on `fmt`
impl fmt::Display for Temperature {
// Define the function `fmt` taking `&self`, `f` and returning a `fmt::Result`
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:.1} degrees", self.0)
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `t`, set to `Temperature(23.456)`
let t = Temperature(23.456);
// Print "{}" to the console, substituting in `t` (with a trailing newline)
println!("{}", t);
}
Login to try C/C++/Java/PHP code in the editor
#[derive(Display)]सेDisplayderive करने की कोशिश करना --Debugके विपरीत,Displayमें कोई built-in derive नहीं और इसे manually implement किया जाना चाहिए।Displayimplement करना लेकिन भूल जाना कि{:?}formatting काम करने के लिएDebugअलग से अब भी चाहिए।fmtसे सीधे एकStringreturn करना बजायwrite!(f, ...)उपयोग करने के, जो Display/Debug output produce करने का सही तरीका है।
Display({}) को manually implement किया जाना चाहिए और यह clean, user-facing output के लिए है।Debug({:?})#[derive(Debug)]से auto-derive हो सकता है और यह developer-facing diagnostics के लिए है।- दोनों traits को एक
fmtmethod define करके implement किया जाता है जोwrite!macro उपयोग करके एकFormatterमें लिखता है। - एक type एक साथ दोनों traits implement कर सकता है,
{}versus{:?}के लिए अलग output प्रदान करते हुए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: