Struct FormattingOptions

pub struct FormattingOptions { pub(in ::fmt) flags: u32, pub(in ::fmt) width: u16, pub(in ::fmt) precision: u16 }

Options for formatting.

FormattingOptions is a Formatter without an attached Write trait. It is mainly used to construct Formatter instances.

Fields

flags: u32

Flags, with the following bit fields:

  31  30  29  28  27  26  25  24  23  22  21  20                              0
┌───┬───────┬───┬───┬───┬───┬───┬───┬───┬───┬──────────────────────────────────┐
│ 0 │ align │ p │ w │ X?│ x?│'0'│ # │ - │ + │               fill               │
└───┴───────┴───┴───┴───┴───┴───┴───┴───┴───┴──────────────────────────────────┘
  │     │     │   │  └─┬───────────────────┘ └─┬──────────────────────────────┘
  │     │     │   │    │                       └─ The fill character (21 bits char).
  │     │     │   │    └─ The debug upper/lower hex, zero pad, alternate, and plus/minus flags.
  │     │     │   └─ Whether a width is set. (The value is stored separately.)
  │     │     └─ Whether a precision is set. (The value is stored separately.)
  │     ├─ 0: Align left. (<)
  │     ├─ 1: Align right. (>)
  │     ├─ 2: Align center. (^)
  │     └─ 3: Alignment not set. (default)
  └─ Always zero.
width: u16

Width if width flag (bit 27) above is set. Otherwise, always 0.

precision: u16

Precision if precision flag (bit 28) above is set. Otherwise, always 0.

Implementations

impl FormattingOptions

const fn new() -> Self

Construct a new FormattingOptions representing the plain {} formatting specifier:

  • no flags,
  • filled with spaces,
  • no alignment,
  • no width,
  • no precision, and
  • no DebugAsHex output mode.
const fn sign(&mut self, sign: Option<Sign>) -> &mut Self

Sets or removes the sign (the + or the - flag).

  • +: This is intended for numeric types and indicates that the sign should always be printed. By default only the negative sign of signed values is printed, and the sign of positive or unsigned values is omitted. This flag indicates that the correct sign (+ or -) should always be printed.
  • -: Currently not used
const fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self

Sets or unsets the 0 flag.

This is used to indicate for integer formats that the padding to width should both be done with a 0 character as well as be sign-aware

const fn alternate(&mut self, alternate: bool) -> &mut Self

Sets or unsets the # flag.

This flag indicates that the "alternate" form of printing should be used. The alternate forms are:

  • Debug : pretty-print the Debug formatting (adds linebreaks and indentation)
  • LowerHex as well as UpperHex - precedes the argument with a 0x
  • Octal - precedes the argument with a 0o
  • Binary - precedes the argument with a 0b
const fn fill(&mut self, fill: char) -> &mut Self

Sets the fill character.

The optional fill character and alignment is provided normally in conjunction with the width parameter. This indicates that if the value being formatted is smaller than width some extra characters will be printed around it.

const fn align(&mut self, align: Option<Alignment>) -> &mut Self

Sets or removes the alignment.

The alignment specifies how the value being formatted should be positioned if it is smaller than the width of the formatter.

const fn width(&mut self, width: Option<u16>) -> &mut Self

Sets or removes the width.

This is a parameter for the “minimum width” that the format should take up. If the value’s string does not fill up this many characters, then the padding specified by FormattingOptions::fill/FormattingOptions::align will be used to take up the required space.

const fn precision(&mut self, precision: Option<u16>) -> &mut Self

Sets or removes the precision.

  • For non-numeric types, this can be considered a “maximum width”. If the resulting string is longer than this width, then it is truncated down to this many characters and that truncated value is emitted with proper fill, alignment and width if those parameters are set.
  • For integral types, this is ignored.
  • For floating-point types, this indicates how many digits after the decimal point should be printed.
const fn debug_as_hex(&mut self, debug_as_hex: Option<DebugAsHex>) -> &mut Self

Specifies whether the Debug trait should use lower-/upper-case hexadecimal or normal integers

const fn get_sign(&self) -> Option<Sign>

Returns the current sign (the + or the - flag).

const fn get_sign_aware_zero_pad(&self) -> bool

Returns the current 0 flag.

const fn get_alternate(&self) -> bool

Returns the current # flag.

const fn get_fill(&self) -> char

Returns the current fill character.

const fn get_align(&self) -> Option<Alignment>

Returns the current alignment.

const fn get_width(&self) -> Option<u16>

Returns the current width.

const fn get_precision(&self) -> Option<u16>

Returns the current precision.

const fn get_debug_as_hex(&self) -> Option<DebugAsHex>

Returns the current x? or X? flag.

const fn create_formatter<'a>(self, write: &'a mut dyn Write + 'a) -> Formatter<'a>

Creates a Formatter that writes its output to the given Write trait.

You may alternatively use [Formatter::new()].

Trait Implementations

impl Clone for FormattingOptions

fn clone(&self) -> FormattingOptions

impl Copy for FormattingOptions

impl Debug for FormattingOptions

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Default for FormattingOptions

fn default() -> Self

Same as [FormattingOptions::new()].

impl Eq for FormattingOptions

fn assert_fields_are_eq(&self)

impl PartialEq for FormattingOptions

fn eq(&self, other: &FormattingOptions) -> bool

impl StructuralPartialEq for FormattingOptions

impl TrivialClone for FormattingOptions

Auto Trait Implementations

impl Freeze for FormattingOptions

impl RefUnwindSafe for FormattingOptions

impl Send for FormattingOptions

impl Sync for FormattingOptions

impl Unpin for FormattingOptions

impl UnsafeUnpin for FormattingOptions

impl UnwindSafe for FormattingOptions

Blanket Implementations

impl<T> Any for FormattingOptions where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for FormattingOptions where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for FormattingOptions where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for FormattingOptions where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for FormattingOptions

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for FormattingOptions where T: Copy + Debug,

impl<T> SizeHint for FormattingOptions where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for FormattingOptions

impl<T, U> Into<U> for FormattingOptions where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for FormattingOptions 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 FormattingOptions where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>