Skip to content

Commit 6ff9409

Browse files
Add more missing examples for Formatter
1 parent 21b5367 commit 6ff9409

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

src/libcore/fmt/mod.rs

+78-3
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,23 @@ impl<'a> Formatter<'a> {
12021202
/// is longer than this length
12031203
///
12041204
/// 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+
/// ```
12051222
#[stable(feature = "rust1", since = "1.0.0")]
12061223
pub fn pad(&mut self, s: &str) -> Result {
12071224
// Make sure there's a fast path up front
@@ -1368,7 +1385,7 @@ impl<'a> Formatter<'a> {
13681385
self.buf.write_str(data)
13691386
}
13701387

1371-
/// Writes some formatted information into this instance
1388+
/// Writes some formatted information into this instance.
13721389
#[stable(feature = "rust1", since = "1.0.0")]
13731390
pub fn write_fmt(&mut self, fmt: Arguments) -> Result {
13741391
write(self.buf, fmt)
@@ -1381,11 +1398,69 @@ impl<'a> Formatter<'a> {
13811398
or `sign_aware_zero_pad` methods instead")]
13821399
pub fn flags(&self) -> u32 { self.flags }
13831400

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+
/// ```
13851428
#[stable(feature = "fmt_flags", since = "1.5.0")]
13861429
pub fn fill(&self) -> char { self.fill }
13871430

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+
/// ```
13891464
#[unstable(feature = "fmt_flags_align", reason = "method was just created",
13901465
issue = "27726")]
13911466
pub fn align(&self) -> Alignment {

0 commit comments

Comments
 (0)