[][src]Crate format

A utility crate to make it easier to work with the Formatter

Examples

Nested debug help struct:

use core::fmt::{Debug, Formatter, Result};
use format::lazy_format;
use std::format;

struct Foo {
    bar: [u32; 10],
}

impl Debug for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let bar = lazy_format!(|f| f.debug_list().entries(&self.bar).finish());
        f.debug_struct("Foo")
            .field("bar", &format_args!("{}", bar))
            .finish()
    }
}

assert_eq!(
    "Foo { bar: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }",
    format!("{:?}", Foo { bar: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] })
);

Control flow:

use core::fmt::{Debug, Formatter, Result};
use format::lazy_format;
use std::format;

struct Foo(usize);

impl Debug for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let alternate = f.alternate();
        let bar = lazy_format!(|f| if alternate {
            write!(f, "{:#x}", self.0)
        } else {
            write!(f, "{:x}", self.0)
        });
        f.debug_tuple("Foo")
            .field(&format_args!("{}", bar))
            .finish()
    }
}

assert_eq!("Foo(75bcd15)", format!("{:?}", Foo(0123456789)));
assert_eq!("Foo(\n    0x75bcd15,\n)", format!("{:#?}", Foo(0123456789)));

Macros

lazy_format

Structs

Binary

A lazy format type that implements Binary as base format trait, Display and Debug as derivative from Binary

Debug

A lazy format type that implements Debug as base format trait

Display

A lazy format type that implements Display as base format trait, Debug as derivative from Display

LowerExp

A lazy format type that implements LowerExp as base format trait, Display and Debug as derivative from LowerExp

LowerHex

A lazy format type that implements LowerHex as base format trait, Display and Debug as derivative from LowerHex

Octal

A lazy format type that implements Octal as base format trait, Display and Debug as derivative from Octal

Pointer

A lazy format type that implements Pointer as base format trait, Display and Debug as derivative from Pointer

UpperExp

A lazy format type that implements UpperExp as base format trait, Display and Debug as derivative from UpperExp

UpperHex

A lazy format type that implements UpperHex as base format trait, Display and Debug as derivative from UpperHex