@@ -1375,27 +1375,159 @@ impl<'a> Formatter<'a> {
13751375 }
13761376 }
13771377
1378- /// Optionally specified integer width that the output should be
1378+ /// Optionally specified integer width that the output should be.
1379+ ///
1380+ /// # Examples
1381+ ///
1382+ /// ```
1383+ /// use std::fmt;
1384+ ///
1385+ /// struct Foo(i32);
1386+ ///
1387+ /// impl fmt::Display for Foo {
1388+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1389+ /// if let Some(width) = formatter.width() {
1390+ /// // If we received a width, we use it
1391+ /// write!(formatter, "{:width$}", &format!("Foo({})", self.0), width = width)
1392+ /// } else {
1393+ /// // Otherwise we do nothing special
1394+ /// write!(formatter, "Foo({})", self.0)
1395+ /// }
1396+ /// }
1397+ /// }
1398+ ///
1399+ /// assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) ");
1400+ /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
1401+ /// ```
13791402 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
13801403 pub fn width ( & self ) -> Option < usize > { self . width }
13811404
1382- /// Optionally specified precision for numeric types
1405+ /// Optionally specified precision for numeric types.
1406+ ///
1407+ /// # Examples
1408+ ///
1409+ /// ```
1410+ /// use std::fmt;
1411+ ///
1412+ /// struct Foo(f32);
1413+ ///
1414+ /// impl fmt::Display for Foo {
1415+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1416+ /// if let Some(precision) = formatter.precision() {
1417+ /// // If we received a precision, we use it.
1418+ /// write!(formatter, "Foo({1:.*})", precision, self.0)
1419+ /// } else {
1420+ /// // Otherwise we default to 2.
1421+ /// write!(formatter, "Foo({:.2})", self.0)
1422+ /// }
1423+ /// }
1424+ /// }
1425+ ///
1426+ /// assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)");
1427+ /// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)");
1428+ /// ```
13831429 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
13841430 pub fn precision ( & self ) -> Option < usize > { self . precision }
13851431
13861432 /// Determines if the `+` flag was specified.
1433+ ///
1434+ /// # Examples
1435+ ///
1436+ /// ```
1437+ /// use std::fmt;
1438+ ///
1439+ /// struct Foo(i32);
1440+ ///
1441+ /// impl fmt::Display for Foo {
1442+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1443+ /// if formatter.sign_plus() {
1444+ /// write!(formatter,
1445+ /// "Foo({}{})",
1446+ /// if self.0 < 0 { '-' } else { '+' },
1447+ /// self.0)
1448+ /// } else {
1449+ /// write!(formatter, "Foo({})", self.0)
1450+ /// }
1451+ /// }
1452+ /// }
1453+ ///
1454+ /// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
1455+ /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
1456+ /// ```
13871457 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
13881458 pub fn sign_plus ( & self ) -> bool { self . flags & ( 1 << FlagV1 :: SignPlus as u32 ) != 0 }
13891459
13901460 /// Determines if the `-` flag was specified.
1461+ ///
1462+ /// # Examples
1463+ ///
1464+ /// ```
1465+ /// use std::fmt;
1466+ ///
1467+ /// struct Foo(i32);
1468+ ///
1469+ /// impl fmt::Display for Foo {
1470+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1471+ /// if formatter.sign_minus() {
1472+ /// // You want a minus sign? Have one!
1473+ /// write!(formatter, "-Foo({})", self.0)
1474+ /// } else {
1475+ /// write!(formatter, "Foo({})", self.0)
1476+ /// }
1477+ /// }
1478+ /// }
1479+ ///
1480+ /// assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)");
1481+ /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
1482+ /// ```
13911483 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
13921484 pub fn sign_minus ( & self ) -> bool { self . flags & ( 1 << FlagV1 :: SignMinus as u32 ) != 0 }
13931485
13941486 /// Determines if the `#` flag was specified.
1487+ ///
1488+ /// # Examples
1489+ ///
1490+ /// ```
1491+ /// use std::fmt;
1492+ ///
1493+ /// struct Foo(i32);
1494+ ///
1495+ /// impl fmt::Display for Foo {
1496+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1497+ /// if formatter.alternate() {
1498+ /// write!(formatter, "Foo({})", self.0)
1499+ /// } else {
1500+ /// write!(formatter, "{}", self.0)
1501+ /// }
1502+ /// }
1503+ /// }
1504+ ///
1505+ /// assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)");
1506+ /// assert_eq!(&format!("{}", Foo(23)), "23");
1507+ /// ```
13951508 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
13961509 pub fn alternate ( & self ) -> bool { self . flags & ( 1 << FlagV1 :: Alternate as u32 ) != 0 }
13971510
13981511 /// Determines if the `0` flag was specified.
1512+ ///
1513+ /// # Examples
1514+ ///
1515+ /// ```
1516+ /// use std::fmt;
1517+ ///
1518+ /// struct Foo(i32);
1519+ ///
1520+ /// impl fmt::Display for Foo {
1521+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1522+ /// assert!(formatter.sign_aware_zero_pad());
1523+ /// assert_eq!(formatter.width(), Some(4));
1524+ /// // We ignore the formatter's options.
1525+ /// write!(formatter, "{}", self.0)
1526+ /// }
1527+ /// }
1528+ ///
1529+ /// assert_eq!(&format!("{:04}", Foo(23)), "23");
1530+ /// ```
13991531 #[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
14001532 pub fn sign_aware_zero_pad ( & self ) -> bool {
14011533 self . flags & ( 1 << FlagV1 :: SignAwareZeroPad as u32 ) != 0
0 commit comments