From 1773e8318f0ff7a928867df0d56c9d58b9b906e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alper=20=C3=87ugun?= Date: Mon, 27 Dec 2021 12:28:11 +0100 Subject: [PATCH 1/2] Add another implementation example to Debug trait --- library/core/src/fmt/mod.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 6fc3cd0b7c4ad..2d4f477ca0189 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -570,11 +570,26 @@ impl Display for Arguments<'_> { /// There are a number of helper methods on the [`Formatter`] struct to help you with manual /// implementations, such as [`debug_struct`]. /// +/// [`debug_struct`]: Formatter::debug_struct +/// +/// For custom cases, it's also possible to implement `Debug` using the [`write!`] macro: +/// ``` +/// # use std::fmt; +/// # struct Point { +/// # x: i32, +/// # y: i32, +/// # } +/// +/// impl fmt::Debug for Point { +/// fn fmt(&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +/// write!(f, "Point [{} {}]", self.x, self.y) +/// } +/// } +/// ``` +/// /// `Debug` implementations using either `derive` or the debug builder API /// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`. /// -/// [`debug_struct`]: Formatter::debug_struct -/// /// Pretty-printing with `#?`: /// /// ``` From 4df1a5561adc0ee6e1635df825d34026e04530b1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 4 Jan 2022 14:25:20 -0800 Subject: [PATCH 2/2] Touch up Debug example from PR 92322 --- library/core/src/fmt/mod.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 2d4f477ca0189..8a2a64f8dc97f 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -572,16 +572,20 @@ impl Display for Arguments<'_> { /// /// [`debug_struct`]: Formatter::debug_struct /// -/// For custom cases, it's also possible to implement `Debug` using the [`write!`] macro: +/// Types that do not wish to use the standard suite of debug representations +/// provided by the `Formatter` trait (`debug_struct`, `debug_tuple`, +/// `debut_list`, `debug_set`, `debug_map`) can do something totally custom by +/// manually writing an arbitrary representation to the `Formatter`. +/// /// ``` /// # use std::fmt; /// # struct Point { /// # x: i32, /// # y: i32, /// # } -/// +/// # /// impl fmt::Debug for Point { -/// fn fmt(&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "Point [{} {}]", self.x, self.y) /// } /// }