Formatting traits

Formatting traits determine how the output of the format will be produced. They are all used in the same way: {:trait_name}.

The following are the current traits available:

Format string

Trait

Meaning

Example

{}

Display

A human-readable representation. Not all things implement Display.

123 => "123"

{:?}

Debug

An internal representation. Almost everything implements Debug.

b"123" => [49, 50, 51]

{:b}

Binary

Converts a number into binary

123 => "1111011"

{:x}

LowerHex

Hex in lowercase

123 => 7b

{:X}

UpperHex

Hex in uppercase

123 => 7B

{:e}

LowerExp

Number with exponential, lowercase

123.0 => 1.23e2

{:E}

UpperExp

Number with exponential, uppercase

123.0 => 1.23E2

{:p}

Pointer

Pointer location

&123 => 0x55b3fbe72980 (may point to a different address on every run)

 

Similarly, the output can be formatted with formatting parameters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset