@@ -49,9 +49,37 @@ impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
4949 }
5050}
5151
52- /// A struct to help with `fmt::Debug` implementations.
52+ /// A struct to help with [ `fmt::Debug`](trait.Debug.html) implementations.
5353///
54- /// Constructed by the `Formatter::debug_struct` method.
54+ /// This is useful when you wish to output a formatted struct as a part of your
55+ /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
56+ ///
57+ /// This can be constructed by the
58+ /// [`Formatter::debug_struct`](struct.Formatter.html#method.debug_struct)
59+ /// method.
60+ ///
61+ /// # Example
62+ ///
63+ /// ```
64+ /// use std::fmt;
65+ ///
66+ /// struct Foo {
67+ /// bar: i32,
68+ /// baz: String,
69+ /// }
70+ ///
71+ /// impl fmt::Debug for Foo {
72+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73+ /// fmt.debug_struct("Foo")
74+ /// .field("bar", &self.bar)
75+ /// .field("baz", &self.baz)
76+ /// .finish()
77+ /// }
78+ /// }
79+ ///
80+ /// // prints "Foo { bar: 10, baz: "Hello World" }"
81+ /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
82+ /// ```
5583#[ must_use]
5684#[ allow( missing_debug_implementations) ]
5785#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
@@ -116,9 +144,34 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
116144 }
117145}
118146
119- /// A struct to help with `fmt::Debug` implementations.
147+ /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
148+ ///
149+ /// This is useful when you wish to output a formatted tuple as a part of your
150+ /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
151+ ///
152+ /// This can be constructed by the
153+ /// [`Formatter::debug_tuple`](struct.Formatter.html#method.debug_tuple)
154+ /// method.
120155///
121- /// Constructed by the `Formatter::debug_tuple` method.
156+ /// # Example
157+ ///
158+ /// ```
159+ /// use std::fmt;
160+ ///
161+ /// struct Foo(i32, String);
162+ ///
163+ /// impl fmt::Debug for Foo {
164+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
165+ /// fmt.debug_tuple("Foo")
166+ /// .field(&self.0)
167+ /// .field(&self.1)
168+ /// .finish()
169+ /// }
170+ /// }
171+ ///
172+ /// // prints "Foo(10, "Hello World")"
173+ /// println!("{:?}", Foo(10, "Hello World".to_string()));
174+ /// ```
122175#[ must_use]
123176#[ allow( missing_debug_implementations) ]
124177#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
@@ -228,9 +281,31 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
228281 }
229282}
230283
231- /// A struct to help with `fmt::Debug` implementations.
284+ /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
285+ ///
286+ /// This is useful when you wish to output a formatted set of items as a part
287+ /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
288+ ///
289+ /// This can be constructed by the
290+ /// [`Formatter::debug_set`](struct.Formatter.html#method.debug_set)
291+ /// method.
292+ ///
293+ /// # Example
232294///
233- /// Constructed by the `Formatter::debug_set` method.
295+ /// ```
296+ /// use std::fmt;
297+ ///
298+ /// struct Foo(Vec<i32>);
299+ ///
300+ /// impl fmt::Debug for Foo {
301+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
302+ /// fmt.debug_set().entries(self.0.iter()).finish()
303+ /// }
304+ /// }
305+ ///
306+ /// // prints "{10, 11}"
307+ /// println!("{:?}", Foo(vec![10, 11]));
308+ /// ```
234309#[ must_use]
235310#[ allow( missing_debug_implementations) ]
236311#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
@@ -277,9 +352,31 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
277352 }
278353}
279354
280- /// A struct to help with `fmt::Debug` implementations.
355+ /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
356+ ///
357+ /// This is useful when you wish to output a formatted list of items as a part
358+ /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
359+ ///
360+ /// This can be constructed by the
361+ /// [`Formatter::debug_list`](struct.Formatter.html#method.debug_list)
362+ /// method.
363+ ///
364+ /// # Example
365+ ///
366+ /// ```
367+ /// use std::fmt;
368+ ///
369+ /// struct Foo(Vec<i32>);
281370///
282- /// Constructed by the `Formatter::debug_list` method.
371+ /// impl fmt::Debug for Foo {
372+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
373+ /// fmt.debug_list().entries(self.0.iter()).finish()
374+ /// }
375+ /// }
376+ ///
377+ /// // prints "[10, 11]"
378+ /// println!("{:?}", Foo(vec![10, 11]));
379+ /// ```
283380#[ must_use]
284381#[ allow( missing_debug_implementations) ]
285382#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
@@ -326,9 +423,31 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> {
326423 }
327424}
328425
329- /// A struct to help with `fmt::Debug` implementations.
426+ /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
427+ ///
428+ /// This is useful when you wish to output a formatted map as a part of your
429+ /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
430+ ///
431+ /// This can be constructed by the
432+ /// [`Formatter::debug_map`](struct.Formatter.html#method.debug_map)
433+ /// method.
434+ ///
435+ /// # Example
436+ ///
437+ /// ```
438+ /// use std::fmt;
439+ ///
440+ /// struct Foo(Vec<(String, i32)>);
441+ ///
442+ /// impl fmt::Debug for Foo {
443+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
444+ /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
445+ /// }
446+ /// }
330447///
331- /// Constructed by the `Formatter::debug_map` method.
448+ /// // prints "{"A": 10, "B": 11}"
449+ /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
450+ /// ```
332451#[ must_use]
333452#[ allow( missing_debug_implementations) ]
334453#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
0 commit comments