@@ -1132,6 +1132,36 @@ impl<'a> Formatter<'a> {
11321132 ///
11331133 /// This function will correctly account for the flags provided as well as
11341134 /// the minimum width. It will not take precision into account.
1135+ ///
1136+ /// # Examples
1137+ ///
1138+ /// ```
1139+ /// use std::fmt;
1140+ ///
1141+ /// struct Foo { nb: i32 };
1142+ ///
1143+ /// impl Foo {
1144+ /// fn new(nb: i32) -> Foo {
1145+ /// Foo {
1146+ /// nb,
1147+ /// }
1148+ /// }
1149+ /// }
1150+ ///
1151+ /// impl fmt::Display for Foo {
1152+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1153+ /// // We need to remove "-" from the number output.
1154+ /// let tmp = self.nb.abs().to_string();
1155+ ///
1156+ /// formatter.pad_integral(self.nb > 0, "Foo ", &tmp)
1157+ /// }
1158+ /// }
1159+ ///
1160+ /// assert_eq!(&format!("{}", Foo::new(2)), "2");
1161+ /// assert_eq!(&format!("{}", Foo::new(-1)), "-1");
1162+ /// assert_eq!(&format!("{:#}", Foo::new(-1)), "-Foo 1");
1163+ /// assert_eq!(&format!("{:0>#8}", Foo::new(-1)), "00-Foo 1");
1164+ /// ```
11351165 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11361166 pub fn pad_integral ( & mut self ,
11371167 is_nonnegative : bool ,
@@ -1381,12 +1411,48 @@ impl<'a> Formatter<'a> {
13811411
13821412 /// Writes some data to the underlying buffer contained within this
13831413 /// formatter.
1414+ ///
1415+ /// # Examples
1416+ ///
1417+ /// ```
1418+ /// use std::fmt;
1419+ ///
1420+ /// struct Foo;
1421+ ///
1422+ /// impl fmt::Display for Foo {
1423+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1424+ /// formatter.write_str("Foo")
1425+ /// // This is equivalent to:
1426+ /// // write!(formatter, "Foo")
1427+ /// }
1428+ /// }
1429+ ///
1430+ /// assert_eq!(&format!("{}", Foo), "Foo");
1431+ /// assert_eq!(&format!("{:0>8}", Foo), "Foo");
1432+ /// ```
13841433 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
13851434 pub fn write_str ( & mut self , data : & str ) -> Result {
13861435 self . buf . write_str ( data)
13871436 }
13881437
13891438 /// Writes some formatted information into this instance.
1439+ ///
1440+ /// # Examples
1441+ ///
1442+ /// ```
1443+ /// use std::fmt;
1444+ ///
1445+ /// struct Foo(i32);
1446+ ///
1447+ /// impl fmt::Display for Foo {
1448+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1449+ /// formatter.write_fmt(format_args!("Foo {}", self.0))
1450+ /// }
1451+ /// }
1452+ ///
1453+ /// assert_eq!(&format!("{}", Foo(-1)), "Foo -1");
1454+ /// assert_eq!(&format!("{:0>8}", Foo(2)), "Foo 2");
1455+ /// ```
13901456 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
13911457 pub fn write_fmt ( & mut self , fmt : Arguments ) -> Result {
13921458 write ( self . buf , fmt)
0 commit comments