format

Crates.ioformat
lib.rsformat
version0.2.4
sourcesrc
created_at2020-04-01 11:36:22.432704
updated_at2021-04-04 14:37:38.693697
descriptionA utility crate to make it easier to work with the formatter
homepagehttps://crates.io/crates/format
repositoryhttps://github.com/kgv/format
max_upload_size
id225155
size26,300
dev (github:oasislabs:dev)

documentation

https://docs.rs/format

README

Format

crates.io docs.rs license ci minimum supported rust version

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

Usage

Add dependency to your Cargo.toml:

[dependencies]
format = "0.2"

and use lazy_format macro:

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()
    }
}

or one of format type:

struct Foo(usize);

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

cargo fmt