Struct std::fmt::Formatter 1.0.0
[−]
[src]
pub struct Formatter<'a> { /* fields omitted */ }
A struct to represent both where to emit formatting strings to and how they should be formatted. A mutable version of this is passed to all formatting traits.
Methods
impl<'a> Formatter<'a>
[src]
impl<'a> Formatter<'a>
pub fn pad_integral(
&mut self,
is_nonnegative: bool,
prefix: &str,
buf: &str
) -> Result<(), Error>
[src]
pub fn pad_integral(
&mut self,
is_nonnegative: bool,
prefix: &str,
buf: &str
) -> Result<(), Error>
Performs the correct padding for an integer which has already been emitted into a str. The str should not contain the sign for the integer, that will be added by this method.
Arguments
- is_nonnegative - whether the original integer was either positive or zero.
- prefix - if the '#' character (Alternate) is provided, this is the prefix to put in front of the number.
- buf - the byte array that the number has been formatted into
This function will correctly account for the flags provided as well as the minimum width. It will not take precision into account.
pub fn pad(&mut self, s: &str) -> Result<(), Error>
[src]
pub fn pad(&mut self, s: &str) -> Result<(), Error>
This function takes a string slice and emits it to the internal buffer after applying the relevant formatting flags specified. The flags recognized for generic strings are:
- width - the minimum width of what to emit
- fill/align - what to emit and where to emit it if the string provided needs to be padded
- precision - the maximum length to emit, the string is truncated if it is longer than this length
Notably this function ignores the flag
parameters.
pub fn write_str(&mut self, data: &str) -> Result<(), Error>
[src]
pub fn write_str(&mut self, data: &str) -> Result<(), Error>
Writes some data to the underlying buffer contained within this formatter.
pub fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
[src]
pub fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes some formatted information into this instance
pub fn flags(&self) -> u32
[src]
pub fn flags(&self) -> u32
: use the sign_plus
, sign_minus
, alternate
, or sign_aware_zero_pad
methods instead
Flags for formatting
pub fn fill(&self) -> char
1.5.0[src]
pub fn fill(&self) -> char
Character used as 'fill' whenever there is alignment
pub fn align(&self) -> Alignment
[src]
pub fn align(&self) -> Alignment
🔬 This is a nightly-only experimental API. (fmt_flags_align
#27726)
method was just created
Flag indicating what form of alignment was requested
pub fn width(&self) -> Option<usize>
1.5.0[src]
pub fn width(&self) -> Option<usize>
Optionally specified integer width that the output should be.
Examples
use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if let Some(width) = formatter.width() { // If we received a width, we use it write!(formatter, "{:width$}", &format!("Foo({})", self.0), width = width) } else { // Otherwise we do nothing special write!(formatter, "Foo({})", self.0) } } } assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) "); assert_eq!(&format!("{}", Foo(23)), "Foo(23)");Run
pub fn precision(&self) -> Option<usize>
1.5.0[src]
pub fn precision(&self) -> Option<usize>
Optionally specified precision for numeric types.
Examples
use std::fmt; struct Foo(f32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if let Some(precision) = formatter.precision() { // If we received a precision, we use it. write!(formatter, "Foo({1:.*})", precision, self.0) } else { // Otherwise we default to 2. write!(formatter, "Foo({:.2})", self.0) } } } assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)"); assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)");Run
pub fn sign_plus(&self) -> bool
1.5.0[src]
pub fn sign_plus(&self) -> bool
Determines if the +
flag was specified.
Examples
use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if formatter.sign_plus() { write!(formatter, "Foo({}{})", if self.0 < 0 { '-' } else { '+' }, self.0) } else { write!(formatter, "Foo({})", self.0) } } } assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)"); assert_eq!(&format!("{}", Foo(23)), "Foo(23)");Run
pub fn sign_minus(&self) -> bool
1.5.0[src]
pub fn sign_minus(&self) -> bool
Determines if the -
flag was specified.
Examples
use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if formatter.sign_minus() { // You want a minus sign? Have one! write!(formatter, "-Foo({})", self.0) } else { write!(formatter, "Foo({})", self.0) } } } assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)"); assert_eq!(&format!("{}", Foo(23)), "Foo(23)");Run
pub fn alternate(&self) -> bool
1.5.0[src]
pub fn alternate(&self) -> bool
Determines if the #
flag was specified.
Examples
use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if formatter.alternate() { write!(formatter, "Foo({})", self.0) } else { write!(formatter, "{}", self.0) } } } assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)"); assert_eq!(&format!("{}", Foo(23)), "23");Run
pub fn sign_aware_zero_pad(&self) -> bool
1.5.0[src]
pub fn sign_aware_zero_pad(&self) -> bool
Determines if the 0
flag was specified.
Examples
use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { assert!(formatter.sign_aware_zero_pad()); assert_eq!(formatter.width(), Some(4)); // We ignore the formatter's options. write!(formatter, "{}", self.0) } } assert_eq!(&format!("{:04}", Foo(23)), "23");Run
pub fn debug_struct(&'b mut self, name: &str) -> DebugStruct<'b, 'a>
1.2.0[src]
pub fn debug_struct(&'b mut self, name: &str) -> DebugStruct<'b, 'a>
Creates a DebugStruct
builder designed to assist with creation of
fmt::Debug
implementations for structs.
Examples
use std::fmt; use std::net::Ipv4Addr; struct Foo { bar: i32, baz: String, addr: Ipv4Addr, } impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("Foo") .field("bar", &self.bar) .field("baz", &self.baz) .field("addr", &format_args!("{}", self.addr)) .finish() } } assert_eq!( "Foo { bar: 10, baz: \"Hello World\", addr: 127.0.0.1 }", format!("{:?}", Foo { bar: 10, baz: "Hello World".to_string(), addr: Ipv4Addr::new(127, 0, 0, 1), }) );Run
pub fn debug_tuple(&'b mut self, name: &str) -> DebugTuple<'b, 'a>
1.2.0[src]
pub fn debug_tuple(&'b mut self, name: &str) -> DebugTuple<'b, 'a>
Creates a DebugTuple
builder designed to assist with creation of
fmt::Debug
implementations for tuple structs.
Examples
use std::fmt; use std::marker::PhantomData; struct Foo<T>(i32, String, PhantomData<T>); impl<T> fmt::Debug for Foo<T> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("Foo") .field(&self.0) .field(&self.1) .field(&format_args!("_")) .finish() } } assert_eq!( "Foo(10, \"Hello\", _)", format!("{:?}", Foo(10, "Hello".to_string(), PhantomData::<u8>)) );Run
pub fn debug_list(&'b mut self) -> DebugList<'b, 'a>
1.2.0[src]
pub fn debug_list(&'b mut self) -> DebugList<'b, 'a>
Creates a DebugList
builder designed to assist with creation of
fmt::Debug
implementations for list-like structures.
Examples
use std::fmt; struct Foo(Vec<i32>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_list().entries(self.0.iter()).finish() } } // prints "[10, 11]" println!("{:?}", Foo(vec![10, 11]));Run
pub fn debug_set(&'b mut self) -> DebugSet<'b, 'a>
1.2.0[src]
pub fn debug_set(&'b mut self) -> DebugSet<'b, 'a>
Creates a DebugSet
builder designed to assist with creation of
fmt::Debug
implementations for set-like structures.
Examples
use std::fmt; struct Foo(Vec<i32>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set().entries(self.0.iter()).finish() } } // prints "{10, 11}" println!("{:?}", Foo(vec![10, 11]));Run
In this more complex example, we use format_args!
and .debug_set()
to build a list of match arms:
use std::fmt; struct Arm<'a, L: 'a, R: 'a>(&'a (L, R)); struct Table<'a, K: 'a, V: 'a>(&'a [(K, V)], V); impl<'a, L, R> fmt::Debug for Arm<'a, L, R> where L: 'a + fmt::Debug, R: 'a + fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { L::fmt(&(self.0).0, fmt)?; fmt.write_str(" => ")?; R::fmt(&(self.0).1, fmt) } } impl<'a, K, V> fmt::Debug for Table<'a, K, V> where K: 'a + fmt::Debug, V: 'a + fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set() .entries(self.0.iter().map(Arm)) .entry(&Arm(&(format_args!("_"), &self.1))) .finish() } }Run
pub fn debug_map(&'b mut self) -> DebugMap<'b, 'a>
1.2.0[src]
pub fn debug_map(&'b mut self) -> DebugMap<'b, 'a>
Creates a DebugMap
builder designed to assist with creation of
fmt::Debug
implementations for map-like structures.
Examples
use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish() } } // prints "{"A": 10, "B": 11}" println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));Run
Trait Implementations
impl<'a> Write for Formatter<'a>
1.2.0[src]
impl<'a> Write for Formatter<'a>
fn write_str(&mut self, s: &str) -> Result<(), Error>
[src]
fn write_str(&mut self, s: &str) -> Result<(), Error>
Writes a slice of bytes into this writer, returning whether the write succeeded. Read more
fn write_char(&mut self, c: char) -> Result<(), Error>
[src]
fn write_char(&mut self, c: char) -> Result<(), Error>
Writes a [char
] into this writer, returning whether the write succeeded. Read more
fn write_fmt(&mut self, args: Arguments) -> Result<(), Error>
[src]
fn write_fmt(&mut self, args: Arguments) -> Result<(), Error>
Glue for usage of the [write!
] macro with implementors of this trait. Read more