Struct Formatter
struct Formatter<'a> { ... }
Configuration for formatting.
A Formatter represents various options related to formatting. Users do not
construct Formatters directly; a mutable reference to one is passed to
the fmt method of all formatting traits, like Debug and Display.
To interact with a Formatter, you'll call various methods to change the
various options related to formatting. For examples, please see the
documentation of the methods defined on Formatter below.
Implementations
impl<'a> Formatter<'a>
const fn new(write: &'a mut dyn Write + 'a, options: FormattingOptions) -> SelfCreates a new formatter with given
FormattingOptions.If
writeis a reference to a formatter, it is recommended to useFormatter::with_optionsinstead as this can borrow the underlyingwrite, thereby bypassing one layer of indirection.You may alternatively use [
FormattingOptions::create_formatter()].const fn with_options<'b>(self: &'b mut Self, options: FormattingOptions) -> Formatter<'b>Creates a new formatter based on this one with given
FormattingOptions.
impl<'a> Formatter<'a>
fn pad_integral(self: &mut Self, is_nonnegative: bool, prefix: &str, buf: &str) -> ResultPerforms 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.
Examples
use fmt; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn pad(self: &mut Self, s: &str) -> ResultTakes 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
flagparameters.Examples
use fmt; ; assert_eq!; assert_eq!;fn write_str(self: &mut Self, data: &str) -> ResultWrites some data to the underlying buffer contained within this formatter.
Examples
use fmt; ; assert_eq!; assert_eq!;fn write_fmt(self: &mut Self, fmt: Arguments<'_>) -> ResultGlue for usage of the [
write!] macro with implementors of this trait.This method should generally not be invoked manually, but rather through the [
write!] macro itself.Writes some formatted information into this instance.
Examples
use fmt; ; assert_eq!; assert_eq!;fn flags(self: &Self) -> u32Returns flags for formatting.
fn fill(self: &Self) -> charReturns the character used as 'fill' whenever there is alignment.
Examples
use fmt; ; // We set alignment to the right with ">". assert_eq!; assert_eq!;fn align(self: &Self) -> Option<Alignment>Returns a flag indicating what form of alignment was requested.
Examples
use ; ; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn width(self: &Self) -> Option<usize>Returns the optionally specified integer width that the output should be.
Examples
use fmt; ; assert_eq!; assert_eq!;fn precision(self: &Self) -> Option<usize>Returns the optionally specified precision for numeric types. Alternatively, the maximum width for string types.
Examples
use fmt; ; assert_eq!; assert_eq!;fn sign_plus(self: &Self) -> boolDetermines if the
+flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!; assert_eq!;fn sign_minus(self: &Self) -> boolDetermines if the
-flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!;fn alternate(self: &Self) -> boolDetermines if the
#flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!;fn sign_aware_zero_pad(self: &Self) -> boolDetermines if the
0flag was specified.Examples
use fmt; ; assert_eq!;fn debug_struct<'b>(self: &'b mut Self, name: &str) -> DebugStruct<'b, 'a>Creates a
DebugStructbuilder designed to assist with creation offmt::Debugimplementations for structs.Examples
use fmt; use Ipv4Addr; assert_eq!;fn debug_tuple<'b>(self: &'b mut Self, name: &str) -> DebugTuple<'b, 'a>Creates a
DebugTuplebuilder designed to assist with creation offmt::Debugimplementations for tuple structs.Examples
use fmt; use PhantomData; ; assert_eq!;fn debug_list<'b>(self: &'b mut Self) -> DebugList<'b, 'a>Creates a
DebugListbuilder designed to assist with creation offmt::Debugimplementations for list-like structures.Examples
use fmt; ; assert_eq!;fn debug_set<'b>(self: &'b mut Self) -> DebugSet<'b, 'a>Creates a
DebugSetbuilder designed to assist with creation offmt::Debugimplementations for set-like structures.Examples
use fmt; ; assert_eq!;In this more complex example, we use
format_args!and.debug_set()to build a list of match arms:use fmt; ); ], V);fn debug_map<'b>(self: &'b mut Self) -> DebugMap<'b, 'a>Creates a
DebugMapbuilder designed to assist with creation offmt::Debugimplementations for map-like structures.Examples
use fmt; >); assert_eq!;const fn sign(self: &Self) -> Option<Sign>Returns the sign of this formatter (
+or-).const fn options(self: &Self) -> FormattingOptionsReturns the formatting options this formatter corresponds to.
impl Write for Formatter<'_>
fn write_str(self: &mut Self, s: &str) -> Resultfn write_char(self: &mut Self, c: char) -> Resultfn write_fmt(self: &mut Self, args: Arguments<'_>) -> Result
impl<'a> Freeze for Formatter<'a>
impl<'a> RefUnwindSafe for Formatter<'a>
impl<'a> Send for Formatter<'a>
impl<'a> Sync for Formatter<'a>
impl<'a> Unpin for Formatter<'a>
impl<'a> UnwindSafe for Formatter<'a>
impl<T> Any for Formatter<'a>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Formatter<'a>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Formatter<'a>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for Formatter<'a>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Formatter<'a>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Formatter<'a>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Formatter<'a>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>