pub trait Debug: PointeeSized {
// Required method
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}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 (std, core, alloc, 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 }",
);Manually 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 }",
);There 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)
}
}Debug implementations using either derive or the debug builder API
on Formatter support pretty-printing using the alternate flag: {:#?}.
Pretty-printing with #?:
Required Methods§
1.0.0 · Sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the value using the given formatter.
§Errors
This function should return Err if, and only if, the provided Formatter returns Err.
String formatting is considered an infallible operation; this function only
returns a Result because writing to the underlying stream might fail and it must
provide a way to propagate the fact that an error has occurred back up the stack.
§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,
)");Implementors§
impl Debug for AsciiChar
impl Debug for core::cmp::Ordering
impl Debug for Infallible
impl Debug for FromBytesWithNulError
impl Debug for c_void
impl Debug for AtomicOrdering
impl Debug for FpCategory
impl Debug for IntErrorKind
impl Debug for SearchStep
impl Debug for core::sync::atomic::Ordering
impl Debug for core::fmt::Alignment
impl Debug for DebugAsHex
impl Debug for Sign
impl Debug for bool
impl Debug for char
impl Debug for f16
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 Layout
impl Debug for TypeId
impl Debug for float16x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float16x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float32x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float32x2x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float32x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float32x4x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for float64x1_t
target_arch=arm64ec only.impl Debug for float64x1x2_t
target_arch=arm64ec only.impl Debug for float64x1x3_t
target_arch=arm64ec only.impl Debug for float64x1x4_t
target_arch=arm64ec only.impl Debug for float64x2_t
target_arch=arm64ec only.impl Debug for float64x2x2_t
target_arch=arm64ec only.impl Debug for float64x2x3_t
target_arch=arm64ec only.impl Debug for float64x2x4_t
target_arch=arm64ec only.impl Debug for int8x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int8x8x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int8x16_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int8x16x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int16x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int16x4x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int16x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int16x8x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int32x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int32x2x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int32x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int32x4x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int64x1_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for int64x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly8x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly8x16_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly16x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly16x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly64x1_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for poly64x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint8x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint8x8x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint8x16_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint16x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint16x8_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint32x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint32x4_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint64x1_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for uint64x2_t
target_arch=arm64ec or target feature v7) and (ARM or AArch64 or target_arch=arm64ec) only.impl Debug for TryFromSliceError
impl Debug for EscapeDefault
impl Debug for ByteStr
impl Debug for BorrowError
impl Debug for BorrowMutError
impl Debug for CharTryFromError
impl Debug for DecodeUtf16Error
impl Debug for EscapeDebug
impl Debug for CStr
Shows the underlying bytes as a normal string, with invalid UTF-8 presented as hex escape sequences.
impl Debug for ParseIntError
impl Debug for TryFromIntError
impl Debug for RangeFull
impl Debug for Location<'_>
impl Debug for PanicMessage<'_>
impl Debug for core::ptr::Alignment
impl Debug for Chars<'_>
impl Debug for Utf8Error
impl Debug for AtomicBool
impl Debug for AtomicU8
impl Debug for AtomicU16
impl Debug for AtomicU32
impl Debug for AtomicU64
impl Debug for AtomicUsize
impl Debug for Duration
impl Debug for TryFromFloatSecsError
impl Debug for Arguments<'_>
impl Debug for Error
impl Debug for FormattingOptions
impl Debug for dyn Any
impl Debug for dyn Any + Send
impl Debug for dyn Any + Send + Sync
impl<'a> Debug for PanicInfo<'a>
impl<'a> Debug for CharSearcher<'a>
impl<'a> Debug for Bytes<'a>
impl<'a> Debug for CharIndices<'a>
impl<'a, 'b> Debug for StrSearcher<'a, 'b>
impl<'a, A: Debug + 'a> Debug for core::option::Iter<'a, A>
impl<'a, A: Debug + 'a> Debug for core::option::IterMut<'a, A>
impl<'a, P: Pattern<Searcher<'a>: Debug>> Debug for SplitInclusive<'a, P>
impl<'a, T: Debug + 'a> Debug for Chunks<'a, T>
impl<'a, T: Debug + 'a> Debug for ChunksExact<'a, T>
impl<'a, T: Debug + 'a> Debug for ChunksExactMut<'a, T>
impl<'a, T: Debug + 'a> Debug for ChunksMut<'a, T>
impl<'a, T: Debug + 'a> Debug for Windows<'a, T>
impl<A: Debug> Debug for core::option::IntoIter<A>
impl<A: Debug, B: Debug> Debug for Chain<A, B>
impl<A: Debug, B: Debug> Debug for Zip<A, B>
impl<B: Debug, C: Debug> Debug for ControlFlow<B, C>
impl<Dyn: PointeeSized> Debug for DynMetadata<Dyn>
impl<F> Debug for core::iter::FromFn<F>
impl<F> Debug for core::fmt::FromFn<F>
impl<I> Debug for DecodeUtf16<I>
impl<I: Debug> Debug for Cloned<I>
impl<I: Debug> Debug for Copied<I>
impl<I: Debug> Debug for Enumerate<I>
impl<I: Debug> Debug for Fuse<I>
impl<I: Debug> Debug for Skip<I>
impl<I: Debug> Debug for StepBy<I>
impl<I: Debug> Debug for Take<I>
impl<I: Debug, F> Debug for Map<I, F>
impl<I: Debug, P> Debug for Filter<I, P>
impl<I: Debug, P> Debug for TakeWhile<I, P>
impl<I: Debug, U, F> Debug for FlatMap<I, U, F>where
U: IntoIterator<IntoIter: Debug>,
impl<Idx: Debug> Debug for Range<Idx>
impl<Idx: Debug> Debug for RangeFrom<Idx>
impl<Idx: Debug> Debug for RangeInclusive<Idx>
impl<Idx: Debug> Debug for RangeTo<Idx>
impl<Idx: Debug> Debug for RangeToInclusive<Idx>
impl<P: Debug + ?Sized> Debug for MaybeDangling<P>
impl<T> Debug for Discriminant<T>
impl<T> Debug for NonZero<T>where
T: ZeroablePrimitive + Debug,
impl<T> Debug for MaybeUninit<T>
impl<T: Copy + Debug> Debug for Cell<T>
impl<T: PointeeSized + Debug> Debug for &T
impl<T: PointeeSized + Debug> Debug for &mut T
impl<T: PointeeSized> Debug for *const T
impl<T: PointeeSized> Debug for *mut T
impl<T: PointeeSized> Debug for NonNull<T>
impl<T: Debug + NumBufferTrait> Debug for NumBuffer<T>
impl<T: Debug + ?Sized> Debug for ManuallyDrop<T>
impl<T: Debug> Debug for Bound<T>
impl<T: Debug> Debug for Option<T>
impl<T: Debug> Debug for [T]
impl<T: Debug> Debug for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.