Expand description
?
formatting.
Debug
should format the output in a programmer-facing, debugging context.
Generally speaking, you should just derive
a Debug
implementation.
When used with the alternate format specifier #?
, the output is pretty-printed.
For more information on formatters, see the module-level documentation.
This trait can be used with #[derive]
if all fields implement Debug
. When
derive
d for structs, it will use the name of the struct
, then {
, then a
comma-separated list of each field’s name and Debug
value, then }
. For
enum
s, it will use the name of the variant and, if applicable, (
, then the
Debug
values of the fields, then )
.
Stability
Derived Debug
formats are not stable, and so may change with future Rust
versions. Additionally, Debug
implementations of types provided by the
standard library (libstd
, libcore
, liballoc
, etc.) are not stable, and
may also change with future Rust versions.
Examples
Deriving an implementation:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {origin:?}"), "The origin is: Point { x: 0, y: 0 }");
RunManually implementing:
use std::fmt;
struct Point {
x: i32,
y: i32,
}
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Point")
.field("x", &self.x)
.field("y", &self.y)
.finish()
}
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {origin:?}"), "The origin is: Point { x: 0, y: 0 }");
RunThere are a number of helper methods on the Formatter
struct to help you with manual
implementations, such as debug_struct
.
Types that do not wish to use the standard suite of debug representations
provided by the Formatter
trait (debug_struct
, debug_tuple
,
debut_list
, debug_set
, debug_map
) can do something totally custom by
manually writing an arbitrary representation to the Formatter
.
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Point [{} {}]", self.x, self.y)
}
}
RunDebug
implementations using either derive
or the debug builder API
on Formatter
support pretty-printing using the alternate flag: {:#?}
.
Pretty-printing with #?
:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {origin:#?}"),
"The origin is: Point {
x: 0,
y: 0,
}");
RunRequired methods
Formats the value using the given formatter.
Examples
use std::fmt;
struct Position {
longitude: f32,
latitude: f32,
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("")
.field(&self.longitude)
.field(&self.latitude)
.finish()
}
}
let position = Position { longitude: 1.987, latitude: 2.983 };
assert_eq!(format!("{position:?}"), "(1.987, 2.983)");
assert_eq!(format!("{position:#?}"), "(
1.987,
2.983,
)");
RunImplementors
impl Debug for core::cmp::Ordering
impl Debug for Infallible
impl Debug for c_void
impl Debug for FpCategory
impl Debug for IntErrorKind
impl Debug for Which
impl Debug for SearchStep
impl Debug for core::sync::atomic::Ordering
impl Debug for Alignment
impl Debug for bool
impl Debug for char
impl Debug for f32
impl Debug for f64
impl Debug for i8
impl Debug for i16
impl Debug for i32
impl Debug for i64
impl Debug for i128
impl Debug for isize
impl Debug for !
impl Debug for str
impl Debug for u8
impl Debug for u16
impl Debug for u32
impl Debug for u64
impl Debug for u128
impl Debug for ()
impl Debug for usize
impl Debug for AllocError
impl Debug for Layout
impl Debug for LayoutError
impl Debug for TypeId
impl Debug for float64x1_t
impl Debug for float64x1x2_t
impl Debug for float64x1x3_t
impl Debug for float64x1x4_t
impl Debug for float64x2_t
impl Debug for float64x2x2_t
impl Debug for float64x2x3_t
impl Debug for float64x2x4_t
impl Debug for int16x2_t
impl Debug for uint16x2_t
impl Debug for float32x2_t
impl Debug for float32x2x2_t
impl Debug for float32x2x3_t
impl Debug for float32x2x4_t
impl Debug for float32x4_t
impl Debug for float32x4x2_t
impl Debug for float32x4x3_t
impl Debug for float32x4x4_t
impl Debug for int8x4_t
impl Debug for int8x8_t
impl Debug for int8x8x2_t
impl Debug for int8x8x3_t
impl Debug for int8x8x4_t
impl Debug for int8x16_t
impl Debug for int8x16x2_t
impl Debug for int8x16x3_t
impl Debug for int8x16x4_t
impl Debug for int16x4_t
impl Debug for int16x4x2_t
impl Debug for int16x4x3_t
impl Debug for int16x4x4_t
impl Debug for int16x8_t
impl Debug for int16x8x2_t
impl Debug for int16x8x3_t
impl Debug for int16x8x4_t
impl Debug for int32x2_t
impl Debug for int32x2x2_t
impl Debug for int32x2x3_t
impl Debug for int32x2x4_t
impl Debug for int32x4_t
impl Debug for int32x4x2_t
impl Debug for int32x4x3_t
impl Debug for int32x4x4_t
impl Debug for int64x1_t
impl Debug for int64x1x2_t
impl Debug for int64x1x3_t
impl Debug for int64x1x4_t
impl Debug for int64x2_t
impl Debug for int64x2x2_t
impl Debug for int64x2x3_t
impl Debug for int64x2x4_t
impl Debug for poly8x8_t
impl Debug for poly8x8x2_t
impl Debug for poly8x8x3_t
impl Debug for poly8x8x4_t
impl Debug for poly8x16_t
impl Debug for poly8x16x2_t
impl Debug for poly8x16x3_t
impl Debug for poly8x16x4_t
impl Debug for poly16x4_t
impl Debug for poly16x4x2_t
impl Debug for poly16x4x3_t
impl Debug for poly16x4x4_t
impl Debug for poly16x8_t
impl Debug for poly16x8x2_t
impl Debug for poly16x8x3_t
impl Debug for poly16x8x4_t
impl Debug for poly64x1_t
impl Debug for poly64x1x2_t
impl Debug for poly64x1x3_t
impl Debug for poly64x1x4_t
impl Debug for poly64x2_t
impl Debug for poly64x2x2_t
impl Debug for poly64x2x3_t
impl Debug for poly64x2x4_t
impl Debug for uint8x4_t
impl Debug for uint8x8_t
impl Debug for uint8x8x2_t
impl Debug for uint8x8x3_t
impl Debug for uint8x8x4_t
impl Debug for uint8x16_t
impl Debug for uint8x16x2_t
impl Debug for uint8x16x3_t
impl Debug for uint8x16x4_t
impl Debug for uint16x4_t
impl Debug for uint16x4x2_t
impl Debug for uint16x4x3_t
impl Debug for uint16x4x4_t
impl Debug for uint16x8_t
impl Debug for uint16x8x2_t
impl Debug for uint16x8x3_t
impl Debug for uint16x8x4_t
impl Debug for uint32x2_t
impl Debug for uint32x2x2_t
impl Debug for uint32x2x3_t
impl Debug for uint32x2x4_t
impl Debug for uint32x4_t
impl Debug for uint32x4x2_t
impl Debug for uint32x4x3_t
impl Debug for uint32x4x4_t
impl Debug for uint64x1_t
impl Debug for uint64x1x2_t
impl Debug for uint64x1x3_t
impl Debug for uint64x1x4_t
impl Debug for uint64x2_t
impl Debug for uint64x2x2_t
impl Debug for uint64x2x3_t
impl Debug for uint64x2x4_t
impl Debug for vector_bool_long
impl Debug for vector_double
impl Debug for vector_signed_long
impl Debug for vector_unsigned_long
impl Debug for v128
target_family="wasm"
only.