@@ -1132,6 +1132,36 @@ impl<'a> Formatter<'a> {
1132
1132
///
1133
1133
/// This function will correctly account for the flags provided as well as
1134
1134
/// 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
+ /// ```
1135
1165
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1136
1166
pub fn pad_integral ( & mut self ,
1137
1167
is_nonnegative : bool ,
@@ -1232,7 +1262,7 @@ impl<'a> Formatter<'a> {
1232
1262
// If our string is longer that the precision, then we must have
1233
1263
// truncation. However other flags like `fill`, `width` and `align`
1234
1264
// must act as always.
1235
- if let Some ( ( i, _) ) = s. char_indices ( ) . skip ( max) . next ( ) {
1265
+ if let Some ( ( i, _) ) = s. char_indices ( ) . nth ( max) {
1236
1266
// LLVM here can't prove that `..i` won't panic `&s[..i]`, but
1237
1267
// we know that it can't panic. Use `get` + `unwrap_or` to avoid
1238
1268
// `unsafe` and otherwise don't emit any panic-related code
@@ -1381,12 +1411,48 @@ impl<'a> Formatter<'a> {
1381
1411
1382
1412
/// Writes some data to the underlying buffer contained within this
1383
1413
/// 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
+ /// ```
1384
1433
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1385
1434
pub fn write_str ( & mut self , data : & str ) -> Result {
1386
1435
self . buf . write_str ( data)
1387
1436
}
1388
1437
1389
1438
/// 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
+ /// ```
1390
1456
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1391
1457
pub fn write_fmt ( & mut self , fmt : Arguments ) -> Result {
1392
1458
write ( self . buf , fmt)
0 commit comments