@@ -406,6 +406,18 @@ impl<'a> Arguments<'a> {
406
406
/// macro validates the format string at compile-time so usage of the [`write`]
407
407
/// and [`format`] functions can be safely performed.
408
408
///
409
+ /// You can use the `Arguments<'a>` that [`format_args!`] returns in `Debug`
410
+ /// and `Display` contexts as seen below. The example also shows that `Debug`
411
+ /// and `Display` format to the same thing: the interpolated format string
412
+ /// in `format_args!`.
413
+ ///
414
+ /// ```rust
415
+ /// let display = format!("{:?}", format_args!("{} foo {:?}", 1, 2));
416
+ /// let debug = format!("{}", format_args!("{} foo {:?}", 1, 2));
417
+ /// assert_eq!("1 foo 2", display);
418
+ /// assert_eq!(display, debug);
419
+ /// ```
420
+ ///
409
421
/// [`format_args!`]: ../../std/macro.format_args.html
410
422
/// [`format`]: ../../std/fmt/fn.format.html
411
423
/// [`write`]: ../../std/fmt/fn.write.html
@@ -1553,23 +1565,32 @@ impl<'a> Formatter<'a> {
1553
1565
///
1554
1566
/// ```rust
1555
1567
/// use std::fmt;
1568
+ /// use std::net::Ipv4Addr;
1556
1569
///
1557
1570
/// struct Foo {
1558
1571
/// bar: i32,
1559
1572
/// baz: String,
1573
+ /// addr: Ipv4Addr,
1560
1574
/// }
1561
1575
///
1562
1576
/// impl fmt::Debug for Foo {
1563
1577
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1564
1578
/// fmt.debug_struct("Foo")
1565
1579
/// .field("bar", &self.bar)
1566
1580
/// .field("baz", &self.baz)
1581
+ /// .field("addr", &format_args!("{}", self.addr))
1567
1582
/// .finish()
1568
1583
/// }
1569
1584
/// }
1570
1585
///
1571
- /// // prints "Foo { bar: 10, baz: "Hello World" }"
1572
- /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
1586
+ /// assert_eq!(
1587
+ /// "Foo { bar: 10, baz: \"Hello World\", addr: 127.0.0.1 }",
1588
+ /// format!("{:?}", Foo {
1589
+ /// bar: 10,
1590
+ /// baz: "Hello World".to_string(),
1591
+ /// addr: Ipv4Addr::new(127, 0, 0, 1),
1592
+ /// })
1593
+ /// );
1573
1594
/// ```
1574
1595
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1575
1596
pub fn debug_struct < ' b > ( & ' b mut self , name : & str ) -> DebugStruct < ' b , ' a > {
@@ -1583,20 +1604,24 @@ impl<'a> Formatter<'a> {
1583
1604
///
1584
1605
/// ```rust
1585
1606
/// use std::fmt;
1607
+ /// use std::marker::PhantomData;
1586
1608
///
1587
- /// struct Foo(i32, String);
1609
+ /// struct Foo<T> (i32, String, PhantomData<T> );
1588
1610
///
1589
- /// impl fmt::Debug for Foo {
1611
+ /// impl<T> fmt::Debug for Foo<T> {
1590
1612
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1591
1613
/// fmt.debug_tuple("Foo")
1592
1614
/// .field(&self.0)
1593
1615
/// .field(&self.1)
1616
+ /// .field(&format_args!("_"))
1594
1617
/// .finish()
1595
1618
/// }
1596
1619
/// }
1597
1620
///
1598
- /// // prints "Foo(10, "Hello World")"
1599
- /// println!("{:?}", Foo(10, "Hello World".to_string()));
1621
+ /// assert_eq!(
1622
+ /// "Foo(10, \"Hello\", _)",
1623
+ /// format!("{:?}", Foo(10, "Hello".to_string(), PhantomData::<u8>))
1624
+ /// );
1600
1625
/// ```
1601
1626
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1602
1627
pub fn debug_tuple < ' b > ( & ' b mut self , name : & str ) -> DebugTuple < ' b , ' a > {
@@ -1646,6 +1671,41 @@ impl<'a> Formatter<'a> {
1646
1671
/// // prints "{10, 11}"
1647
1672
/// println!("{:?}", Foo(vec![10, 11]));
1648
1673
/// ```
1674
+ ///
1675
+ /// [`format_args!`]: ../../std/macro.format_args.html
1676
+ ///
1677
+ /// In this more complex example, we use [`format_args!`] and `.debug_set()`
1678
+ /// to build a list of match arms:
1679
+ ///
1680
+ /// ```rust
1681
+ /// use std::fmt;
1682
+ ///
1683
+ /// struct Arm<'a, L: 'a, R: 'a>(&'a (L, R));
1684
+ /// struct Table<'a, K: 'a, V: 'a>(&'a [(K, V)], V);
1685
+ ///
1686
+ /// impl<'a, L, R> fmt::Debug for Arm<'a, L, R>
1687
+ /// where
1688
+ /// L: 'a + fmt::Debug, R: 'a + fmt::Debug
1689
+ /// {
1690
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1691
+ /// L::fmt(&(self.0).0, fmt)?;
1692
+ /// fmt.write_str(" => ")?;
1693
+ /// R::fmt(&(self.0).1, fmt)
1694
+ /// }
1695
+ /// }
1696
+ ///
1697
+ /// impl<'a, K, V> fmt::Debug for Table<'a, K, V>
1698
+ /// where
1699
+ /// K: 'a + fmt::Debug, V: 'a + fmt::Debug
1700
+ /// {
1701
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1702
+ /// fmt.debug_set()
1703
+ /// .entries(self.0.iter().map(Arm))
1704
+ /// .entry(&Arm(&(format_args!("_"), &self.1)))
1705
+ /// .finish()
1706
+ /// }
1707
+ /// }
1708
+ /// ```
1649
1709
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1650
1710
pub fn debug_set < ' b > ( & ' b mut self ) -> DebugSet < ' b , ' a > {
1651
1711
builders:: debug_set_new ( self )
0 commit comments