Struct Formatter
pub struct Formatter<'a> { pub(in ::fmt) options: FormattingOptions, pub(in ::fmt) buf: &'a mut dyn Write + '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.
Fields
options: FormattingOptionsbuf: &'a mut dyn Write + 'a
Implementations
impl<'a> Formatter<'a>
fn wrap_buf<'b, 'c, F>(&'b mut self, wrap: F) -> Formatter<'c> where F: FnOnce(&'b mut dyn Write + 'b) -> &'c mut dyn Write + 'c, 'b: 'c,fn pad_integral(&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(&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 padding(&mut self, padding: u16, default: Alignment) -> Result<PostPadding, Error>Writes the pre-padding and returns the unwritten post-padding.
Callers are responsible for ensuring post-padding is written after the thing that is being padded.
unsafe fn pad_formatted_parts(&mut self, formatted: &Formatted<'_>) -> ResultTakes the formatted parts and applies the padding.
Assumes that the caller already has rendered the parts with required precision, so that
self.precisioncan be ignored.Safety
Any
numfmt::Part::Copyparts informattedmust contain valid UTF-8.unsafe fn write_formatted_parts(&mut self, formatted: &Formatted<'_>) -> ResultSafety
Any
numfmt::Part::Copyparts informattedmust contain valid UTF-8.fn write_str(&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(&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) -> u32Returns flags for formatting.
fn fill(&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) -> 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) -> Option<usize>Returns the optionally specified integer width that the output should be.
Examples
use fmt; ; assert_eq!; assert_eq!;fn precision(&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) -> boolDetermines if the
+flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!; assert_eq!;fn sign_minus(&self) -> boolDetermines if the
-flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!;fn alternate(&self) -> boolDetermines if the
#flag was specified.Examples
use fmt; ; assert_eq!; assert_eq!;fn sign_aware_zero_pad(&self) -> boolDetermines if the
0flag was specified.Examples
use fmt; ; assert_eq!;fn debug_lower_hex(&self) -> boolfn debug_upper_hex(&self) -> boolfn debug_struct<'b>(&'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_struct_field1_finish<'b>(&'b mut self, name: &str, name1: &str, value1: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_struct_fields_finishis more general, but this is faster for 1 field.fn debug_struct_field2_finish<'b>(&'b mut self, name: &str, name1: &str, value1: &dyn Debug, name2: &str, value2: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_struct_fields_finishis more general, but this is faster for 2 fields.fn debug_struct_field3_finish<'b>(&'b mut self, name: &str, name1: &str, value1: &dyn Debug, name2: &str, value2: &dyn Debug, name3: &str, value3: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_struct_fields_finishis more general, but this is faster for 3 fields.fn debug_struct_field4_finish<'b>(&'b mut self, name: &str, name1: &str, value1: &dyn Debug, name2: &str, value2: &dyn Debug, name3: &str, value3: &dyn Debug, name4: &str, value4: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_struct_fields_finishis more general, but this is faster for 4 fields.fn debug_struct_field5_finish<'b>(&'b mut self, name: &str, name1: &str, value1: &dyn Debug, name2: &str, value2: &dyn Debug, name3: &str, value3: &dyn Debug, name4: &str, value4: &dyn Debug, name5: &str, value5: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_struct_fields_finishis more general, but this is faster for 5 fields.fn debug_struct_fields_finish<'b>(&'b mut self, name: &str, names: &[&str], values: &[&dyn Debug]) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries. For the cases not covered bydebug_struct_field[12345]_finish.fn debug_c_like_enum_write_str<'b>(&'b mut self, names: &str, offset: &[usize], discr: usize) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries. For C-like enums with concatenated variant name strings.fn debug_tuple<'b>(&'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_tuple_field1_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_tuple_fields_finishis more general, but this is faster for 1 field.fn debug_tuple_field2_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug, value2: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_tuple_fields_finishis more general, but this is faster for 2 fields.fn debug_tuple_field3_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug, value2: &dyn Debug, value3: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_tuple_fields_finishis more general, but this is faster for 3 fields.fn debug_tuple_field4_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug, value2: &dyn Debug, value3: &dyn Debug, value4: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_tuple_fields_finishis more general, but this is faster for 4 fields.fn debug_tuple_field5_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug, value2: &dyn Debug, value3: &dyn Debug, value4: &dyn Debug, value5: &dyn Debug) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries.debug_tuple_fields_finishis more general, but this is faster for 5 fields.fn debug_tuple_fields_finish<'b>(&'b mut self, name: &str, values: &[&dyn Debug]) -> ResultShrinks
derive(Debug)code, for faster compilation and smaller binaries. For the cases not covered bydebug_tuple_field[12345]_finish.fn debug_list<'b>(&'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>(&'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>(&'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) -> Option<Sign>Returns the sign of this formatter (
+or-).const fn options(&self) -> FormattingOptionsReturns the formatting options this formatter corresponds to.
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>(&'b mut self, options: FormattingOptions) -> Formatter<'b>Creates a new formatter based on this one with given
FormattingOptions.
Trait Implementations
impl Write for Formatter<'_>
fn write_str(&mut self, s: &str) -> Resultfn write_char(&mut self, c: char) -> Resultfn write_fmt(&mut self, args: Arguments<'_>) -> Result
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Formatter<'a>
impl<'a> !Send for Formatter<'a>
impl<'a> !Sync for Formatter<'a>
impl<'a> !UnwindSafe for Formatter<'a>
impl<'a> Freeze for Formatter<'a>
impl<'a> Unpin for Formatter<'a>
impl<'a> UnsafeUnpin for Formatter<'a>
Blanket Implementations
impl<T> Any for Formatter<'a>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Formatter<'a>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Formatter<'a>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Formatter<'a>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SizeHint for Formatter<'a>
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for Formatter<'a>
impl<T, U> Into<U> for Formatter<'a>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Formatter<'a>
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Formatter<'a>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>