@@ -1202,6 +1202,23 @@ impl<'a> Formatter<'a> {
1202
1202
/// is longer than this length
1203
1203
///
1204
1204
/// Notably this function ignores the `flag` parameters.
1205
+ ///
1206
+ /// # Examples
1207
+ ///
1208
+ /// ```
1209
+ /// use std::fmt;
1210
+ ///
1211
+ /// struct Foo;
1212
+ ///
1213
+ /// impl fmt::Display for Foo {
1214
+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1215
+ /// formatter.pad("Foo")
1216
+ /// }
1217
+ /// }
1218
+ ///
1219
+ /// assert_eq!(&format!("{:<4}", Foo), "Foo ");
1220
+ /// assert_eq!(&format!("{:0>4}", Foo), "0Foo");
1221
+ /// ```
1205
1222
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1206
1223
pub fn pad ( & mut self , s : & str ) -> Result {
1207
1224
// Make sure there's a fast path up front
@@ -1368,7 +1385,7 @@ impl<'a> Formatter<'a> {
1368
1385
self . buf . write_str ( data)
1369
1386
}
1370
1387
1371
- /// Writes some formatted information into this instance
1388
+ /// Writes some formatted information into this instance.
1372
1389
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1373
1390
pub fn write_fmt ( & mut self , fmt : Arguments ) -> Result {
1374
1391
write ( self . buf , fmt)
@@ -1381,11 +1398,69 @@ impl<'a> Formatter<'a> {
1381
1398
or `sign_aware_zero_pad` methods instead") ]
1382
1399
pub fn flags ( & self ) -> u32 { self . flags }
1383
1400
1384
- /// Character used as 'fill' whenever there is alignment
1401
+ /// Character used as 'fill' whenever there is alignment.
1402
+ ///
1403
+ /// # Examples
1404
+ ///
1405
+ /// ```
1406
+ /// use std::fmt;
1407
+ ///
1408
+ /// struct Foo;
1409
+ ///
1410
+ /// impl fmt::Display for Foo {
1411
+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1412
+ /// let c = formatter.fill();
1413
+ /// if let Some(width) = formatter.width() {
1414
+ /// for _ in 0..width {
1415
+ /// write!(formatter, "{}", c)?;
1416
+ /// }
1417
+ /// Ok(())
1418
+ /// } else {
1419
+ /// write!(formatter, "{}", c)
1420
+ /// }
1421
+ /// }
1422
+ /// }
1423
+ ///
1424
+ /// // We set alignment to the left with ">".
1425
+ /// assert_eq!(&format!("{:G>3}", Foo), "GGG");
1426
+ /// assert_eq!(&format!("{:t>6}", Foo), "tttttt");
1427
+ /// ```
1385
1428
#[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
1386
1429
pub fn fill ( & self ) -> char { self . fill }
1387
1430
1388
- /// Flag indicating what form of alignment was requested
1431
+ /// Flag indicating what form of alignment was requested.
1432
+ ///
1433
+ /// # Examples
1434
+ ///
1435
+ /// ```
1436
+ /// #![feature(fmt_flags_align)]
1437
+ ///
1438
+ /// extern crate core;
1439
+ ///
1440
+ /// use std::fmt;
1441
+ /// use core::fmt::Alignment;
1442
+ ///
1443
+ /// struct Foo;
1444
+ ///
1445
+ /// impl fmt::Display for Foo {
1446
+ /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1447
+ /// let s = match formatter.align() {
1448
+ /// Alignment::Left => "left",
1449
+ /// Alignment::Right => "right",
1450
+ /// Alignment::Center => "center",
1451
+ /// Alignment::Unknown => "into the void",
1452
+ /// };
1453
+ /// write!(formatter, "{}", s)
1454
+ /// }
1455
+ /// }
1456
+ ///
1457
+ /// fn main() {
1458
+ /// assert_eq!(&format!("{:<}", Foo), "left");
1459
+ /// assert_eq!(&format!("{:>}", Foo), "right");
1460
+ /// assert_eq!(&format!("{:^}", Foo), "center");
1461
+ /// assert_eq!(&format!("{}", Foo), "into the void");
1462
+ /// }
1463
+ /// ```
1389
1464
#[ unstable( feature = "fmt_flags_align" , reason = "method was just created" ,
1390
1465
issue = "27726" ) ]
1391
1466
pub fn align ( & self ) -> Alignment {
0 commit comments