Struct core::fmt::Formatter
[−]
[src]
pub struct Formatter<'a> { // some 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>
fn pad_integral(&mut self, is_positive: bool, prefix: &str, buf: &str) -> Result
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_positive - whether the original integer was positive or not.
- 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.
fn pad(&mut self, s: &str) -> Result
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 ignored the flag
parameters
fn write_str(&mut self, data: &str) -> Result
Writes some data to the underlying buffer contained within this formatter.
fn write_fmt(&mut self, fmt: Arguments) -> Result
Writes some formatted information into this instance
fn flags(&self) -> u32
Flags for formatting (packed version of rt::Flag)
fn fill(&self) -> char
Character used as 'fill' whenever there is alignment
fn align(&self) -> Alignment
Flag indicating what form of alignment was requested
fn width(&self) -> Option<usize>
Optionally specified integer width that the output should be
fn precision(&self) -> Option<usize>
Optionally specified precision for numeric types
fn sign_plus(&self) -> bool
Determines if the +
flag was specified.
fn sign_minus(&self) -> bool
Determines if the -
flag was specified.
fn alternate(&self) -> bool
Determines if the #
flag was specified.
fn sign_aware_zero_pad(&self) -> bool
Determines if the 0
flag was specified.
fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a>
Creates a DebugStruct
builder designed to assist with creation of
fmt::Debug
implementations for structs.
Examples
fn main() { use std::fmt; struct Foo { bar: i32, baz: String, } 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) .finish() } } // prints "Foo { bar: 10, baz: "Hello World" }" println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() }); }use std::fmt; struct Foo { bar: i32, baz: String, } 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) .finish() } } // prints "Foo { bar: 10, baz: "Hello World" }" println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
fn debug_tuple<'b>(&'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
fn main() { use std::fmt; struct Foo(i32, String); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("Foo") .field(&self.0) .field(&self.1) .finish() } } // prints "Foo(10, "Hello World")" println!("{:?}", Foo(10, "Hello World".to_string())); }use std::fmt; struct Foo(i32, String); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("Foo") .field(&self.0) .field(&self.1) .finish() } } // prints "Foo(10, "Hello World")" println!("{:?}", Foo(10, "Hello World".to_string()));
fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a>
Creates a DebugList
builder designed to assist with creation of
fmt::Debug
implementations for list-like structures.
Examples
fn main() { 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])); }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]));
fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a>
Creates a DebugSet
builder designed to assist with creation of
fmt::Debug
implementations for set-like structures.
Examples
fn main() { 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])); }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]));
fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a>
Creates a DebugMap
builder designed to assist with creation of
fmt::Debug
implementations for map-like structures.
Examples
fn main() { 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)])); }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)]));