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
derived 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
enums, 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,
debug_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
sourceimpl Debug for Infallible
1.34.0 · sourceimpl Debug for c_void
1.16.0 · sourceimpl Debug for FpCategory
sourceimpl Debug for IntErrorKind
1.55.0 · sourceimpl Debug for Which
sourceimpl Debug for SearchStep
sourceimpl Debug for core::sync::atomic::Ordering
sourceimpl Debug for Alignment
1.28.0 · sourceimpl Debug for bool
sourceimpl Debug for char
sourceimpl Debug for f32
sourceimpl Debug for f64
sourceimpl Debug for i8
sourceimpl Debug for i16
sourceimpl Debug for i32
sourceimpl Debug for i64
sourceimpl Debug for i128
sourceimpl Debug for isize
sourceimpl Debug for !
sourceimpl Debug for str
sourceimpl Debug for u8
sourceimpl Debug for u16
sourceimpl Debug for u32
sourceimpl Debug for u64
sourceimpl Debug for u128
sourceimpl Debug for ()
sourceimpl Debug for usize
sourceimpl Debug for AllocError
sourceimpl Debug for Layout
1.28.0 · sourceimpl Debug for LayoutError
1.50.0 · sourceimpl Debug for TypeId
sourceimpl Debug for float64x1_t
1.59.0 · sourceimpl Debug for float64x1x2_t
1.59.0 · sourceimpl Debug for float64x1x3_t
1.59.0 · sourceimpl Debug for float64x1x4_t
1.59.0 · sourceimpl Debug for float64x2_t
1.59.0 · sourceimpl Debug for float64x2x2_t
1.59.0 · sourceimpl Debug for float64x2x3_t
1.59.0 · sourceimpl Debug for float64x2x4_t
1.59.0 · sourceimpl Debug for int16x2_t
sourceimpl Debug for uint16x2_t
sourceimpl Debug for float32x2_t
sourceimpl Debug for float32x2x2_t
sourceimpl Debug for float32x2x3_t
sourceimpl Debug for float32x2x4_t
sourceimpl Debug for float32x4_t
sourceimpl Debug for float32x4x2_t
sourceimpl Debug for float32x4x3_t
sourceimpl Debug for float32x4x4_t
sourceimpl Debug for int8x4_t
sourceimpl Debug for int8x8_t
sourceimpl Debug for int8x8x2_t
sourceimpl Debug for int8x8x3_t
sourceimpl Debug for int8x8x4_t
sourceimpl Debug for int8x16_t
sourceimpl Debug for int8x16x2_t
sourceimpl Debug for int8x16x3_t
sourceimpl Debug for int8x16x4_t
sourceimpl Debug for int16x4_t
sourceimpl Debug for int16x4x2_t
sourceimpl Debug for int16x4x3_t
sourceimpl Debug for int16x4x4_t
sourceimpl Debug for int16x8_t
sourceimpl Debug for int16x8x2_t
sourceimpl Debug for int16x8x3_t
sourceimpl Debug for int16x8x4_t
sourceimpl Debug for int32x2_t
sourceimpl Debug for int32x2x2_t
sourceimpl Debug for int32x2x3_t
sourceimpl Debug for int32x2x4_t
sourceimpl Debug for int32x4_t
sourceimpl Debug for int32x4x2_t
sourceimpl Debug for int32x4x3_t
sourceimpl Debug for int32x4x4_t
sourceimpl Debug for int64x1_t
sourceimpl Debug for int64x1x2_t
sourceimpl Debug for int64x1x3_t
sourceimpl Debug for int64x1x4_t
sourceimpl Debug for int64x2_t
sourceimpl Debug for int64x2x2_t
sourceimpl Debug for int64x2x3_t
sourceimpl Debug for int64x2x4_t
sourceimpl Debug for poly8x8_t
sourceimpl Debug for poly8x8x2_t
sourceimpl Debug for poly8x8x3_t
sourceimpl Debug for poly8x8x4_t
sourceimpl Debug for poly8x16_t
sourceimpl Debug for poly8x16x2_t
sourceimpl Debug for poly8x16x3_t
sourceimpl Debug for poly8x16x4_t
sourceimpl Debug for poly16x4_t
sourceimpl Debug for poly16x4x2_t
sourceimpl Debug for poly16x4x3_t
sourceimpl Debug for poly16x4x4_t
sourceimpl Debug for poly16x8_t
sourceimpl Debug for poly16x8x2_t
sourceimpl Debug for poly16x8x3_t
sourceimpl Debug for poly16x8x4_t
sourceimpl Debug for poly64x1_t
sourceimpl Debug for poly64x1x2_t
sourceimpl Debug for poly64x1x3_t
sourceimpl Debug for poly64x1x4_t
sourceimpl Debug for poly64x2_t
sourceimpl Debug for poly64x2x2_t
sourceimpl Debug for poly64x2x3_t
sourceimpl Debug for poly64x2x4_t
sourceimpl Debug for uint8x4_t
sourceimpl Debug for uint8x8_t
sourceimpl Debug for uint8x8x2_t
sourceimpl Debug for uint8x8x3_t
sourceimpl Debug for uint8x8x4_t
sourceimpl Debug for uint8x16_t
sourceimpl Debug for uint8x16x2_t
sourceimpl Debug for uint8x16x3_t
sourceimpl Debug for uint8x16x4_t
sourceimpl Debug for uint16x4_t
sourceimpl Debug for uint16x4x2_t
sourceimpl Debug for uint16x4x3_t
sourceimpl Debug for uint16x4x4_t
sourceimpl Debug for uint16x8_t
sourceimpl Debug for uint16x8x2_t
sourceimpl Debug for uint16x8x3_t
sourceimpl Debug for uint16x8x4_t
sourceimpl Debug for uint32x2_t
sourceimpl Debug for uint32x2x2_t
sourceimpl Debug for uint32x2x3_t
sourceimpl Debug for uint32x2x4_t
sourceimpl Debug for uint32x4_t
sourceimpl Debug for uint32x4x2_t
sourceimpl Debug for uint32x4x3_t
sourceimpl Debug for uint32x4x4_t
sourceimpl Debug for uint64x1_t
sourceimpl Debug for uint64x1x2_t
sourceimpl Debug for uint64x1x3_t
sourceimpl Debug for uint64x1x4_t
sourceimpl Debug for uint64x2_t
sourceimpl Debug for uint64x2x2_t
sourceimpl Debug for uint64x2x3_t
sourceimpl Debug for uint64x2x4_t
sourceimpl Debug for vector_bool_long
sourceimpl Debug for vector_double
sourceimpl Debug for vector_signed_long
sourceimpl Debug for vector_unsigned_long
sourceimpl Debug for v128
1.54.0 · sourcetarget_family="wasm" only.