pub struct Formatter<'a> { /* private fields */ }Expand description
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 Debug.
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.
Implementations§
Source§impl<'a> Formatter<'a>
impl<'a> Formatter<'a>
Sourcepub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a>
pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a>
Creates a DebugStruct builder designed to assist with creation of
Debug implementations for structs.
§Examples
use debug2::{pprint, Debug, Formatter};
use std::fmt;
use std::net::Ipv4Addr;
struct Foo {
bar: i32,
baz: String,
addr: Ipv4Addr,
}
impl Debug for Foo {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("Foo")
.field("bar", &self.bar)
.field("baz", &self.baz)
.field("addr", &format!("{}", self.addr))
.finish()
}
}
assert_eq!(
"Foo { bar: 10, baz: \"Hello World\", addr: \"127.0.0.1\" }",
pprint(Foo {
bar: 10,
baz: "Hello World".to_string(),
addr: Ipv4Addr::new(127, 0, 0, 1),
})
);Sourcepub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a>
pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a>
Creates a DebugTuple builder designed to assist with creation of
Debug implementations for tuple structs.
§Examples
use debug2::{pprint, Debug, Formatter};
use std::fmt;
use std::marker::PhantomData;
struct Foo<T>(i32, String, PhantomData<T>);
impl<T> Debug for Foo<T> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_tuple("Foo")
.field(&self.0)
.field(&self.1)
.field(&format!("_"))
.finish()
}
}
assert_eq!(
"Foo(10, \"Hello\", \"_\")",
pprint(Foo(10, "Hello".to_string(), PhantomData::<u8>))
);Sourcepub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a>
pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a>
Creates a DebugList builder designed to assist with creation of
Debug implementations for list-like structures.
§Examples
use debug2::{pprint, Debug, Formatter};
use std::fmt;
struct Foo(Vec<i32>);
impl Debug for Foo {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_list().entries(self.0.iter()).finish()
}
}
assert_eq!(pprint(Foo(vec![10, 11])), "[10, 11]");Sourcepub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a>
pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a>
Creates a DebugSet builder designed to assist with creation of
Debug implementations for set-like structures.
§Examples
use debug2::{pprint, Debug, Formatter};
use std::fmt;
struct Foo(Vec<i32>);
impl Debug for Foo {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_set().entries(self.0.iter()).finish()
}
}
assert_eq!(pprint(Foo(vec![10, 11])), "{10, 11}");Sourcepub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a>
pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a>
Creates a DebugMap builder designed to assist with creation of
Debug implementations for map-like structures.
§Examples
use debug2::{pprint, Debug, Formatter};
use std::fmt;
struct Foo(Vec<(String, i32)>);
impl Debug for Foo {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_map()
.entries(self.0.iter().map(|&(ref k, ref v)| (k, v)))
.finish()
}
}
assert_eq!(
pprint(Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
r#"{"A": 10, "B": 11}"#
);