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
,
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
impl Debug for Infallible
impl Debug for Which
impl Debug for c_void
impl Debug for FpCategory
impl Debug for IntErrorKind
impl Debug for core::sync::atomic::Ordering
impl Debug for TryReserveErrorKind
impl Debug for SearchStep
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 TypeId
impl Debug for TryFromSliceError
impl Debug for core::ascii::EscapeDefault
impl Debug for BorrowError
impl Debug for BorrowMutError
impl Debug for CharTryFromError
impl Debug for ParseCharError
impl Debug for DecodeUtf16Error
impl Debug for core::char::EscapeDebug
impl Debug for core::char::EscapeDefault
impl Debug for core::char::EscapeUnicode
impl Debug for ToLowercase
impl Debug for ToUppercase
impl Debug for TryFromCharError
impl Debug for CpuidResult
impl Debug for __m128
impl Debug for __m128bh
impl Debug for __m128d
impl Debug for __m128i
impl Debug for __m256
impl Debug for __m256bh
impl Debug for __m256d
impl Debug for __m256i
impl Debug for __m512
impl Debug for __m512bh
impl Debug for __m512d
impl Debug for __m512i
impl Debug for CStr
impl Debug for FromBytesUntilNulError
impl Debug for FromBytesWithNulError
impl Debug for SipHasher
impl Debug for PhantomPinned
impl Debug for Assume
impl Debug for ParseFloatError
impl Debug for ParseIntError
impl Debug for TryFromIntError
impl Debug for NonZeroI8
impl Debug for NonZeroI16
impl Debug for NonZeroI32
impl Debug for NonZeroI64
impl Debug for NonZeroI128
impl Debug for NonZeroIsize
impl Debug for NonZeroU8
impl Debug for NonZeroU16
impl Debug for NonZeroU32
impl Debug for NonZeroU64
impl Debug for NonZeroU128
impl Debug for NonZeroUsize
impl Debug for RangeFull
impl Debug for AtomicBool
impl Debug for AtomicI8
impl Debug for AtomicI16
impl Debug for AtomicI32
impl Debug for AtomicI64
impl Debug for AtomicIsize
impl Debug for AtomicU8
impl Debug for AtomicU16
impl Debug for AtomicU32
impl Debug for AtomicU64
impl Debug for AtomicUsize
impl Debug for Context<'_>
impl Debug for RawWaker
impl Debug for RawWakerVTable
impl Debug for Waker
impl Debug for Duration
impl Debug for FromFloatSecsError
impl Debug for AllocError
impl Debug for Global
impl Debug for Layout
impl Debug for LayoutError
impl Debug for TryReserveError
impl Debug for CString
impl Debug for FromVecWithNulError
impl Debug for IntoStringError
impl Debug for NulError
impl Debug for Chars<'_>
impl Debug for EncodeUtf16<'_>
impl Debug for ParseBoolError
impl Debug for Utf8Chunks<'_>
impl Debug for Utf8Error
impl Debug for alloc::string::Drain<'_>
impl Debug for FromUtf8Error
impl Debug for FromUtf16Error
impl Debug for String
impl Debug for Arguments<'_>
impl Debug for Error
impl Debug for dyn Any + 'static
impl Debug for dyn Any + Send + 'static
impl Debug for dyn Any + Sync + Send + 'static
impl<'a> Debug for Demand<'a>
impl<'a> Debug for Source<'a>
impl<'a> Debug for Location<'a>
impl<'a> Debug for PanicInfo<'a>
impl<'a> Debug for EscapeAscii<'a>
impl<'a> Debug for CharSearcher<'a>
impl<'a> Debug for Bytes<'a>
impl<'a> Debug for CharIndices<'a>
impl<'a> Debug for alloc::str::EscapeDebug<'a>
impl<'a> Debug for alloc::str::EscapeDefault<'a>
impl<'a> Debug for alloc::str::EscapeUnicode<'a>
impl<'a> Debug for Lines<'a>
impl<'a> Debug for LinesAny<'a>
impl<'a> Debug for SplitAsciiWhitespace<'a>
impl<'a> Debug for SplitWhitespace<'a>
impl<'a> Debug for Utf8Chunk<'a>
impl<'a, 'b> Debug for CharSliceSearcher<'a, 'b>
impl<'a, 'b> Debug for StrSearcher<'a, 'b>
impl<'a, 'b, const N: usize> Debug for CharArrayRefSearcher<'a, 'b, N>
impl<'a, 'f> Debug for VaList<'a, 'f> where
'f: 'a,
impl<'a, A> Debug for core::option::Iter<'a, A> where
A: 'a + Debug,
impl<'a, A> Debug for core::option::IterMut<'a, A> where
A: 'a + Debug,
impl<'a, I> Debug for ByRefSized<'a, I> where
I: Debug,
impl<'a, I: Debug + Iterator + 'a, A: Debug + Allocator + 'a> Debug for Splice<'a, I, A> where
I::Item: Debug,
impl<'a, P> Debug for MatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for Matches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for RMatches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::RSplit<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::RSplitN<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::Split<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::SplitInclusive<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::SplitN<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for SplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, T> Debug for core::result::Iter<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for core::result::IterMut<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for Chunks<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for ChunksExact<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for ChunksExactMut<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for ChunksMut<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for RChunks<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for RChunksExact<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for RChunksExactMut<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for RChunksMut<'a, T> where
T: 'a + Debug,
impl<'a, T> Debug for Windows<'a, T> where
T: 'a + Debug,
impl<'a, T, P> Debug for GroupBy<'a, T, P> where
T: 'a + Debug,
impl<'a, T, P> Debug for GroupByMut<'a, T, P> where
T: 'a + Debug,
impl<'a, T, const N: usize> Debug for alloc::slice::ArrayChunks<'a, T, N> where
T: 'a + Debug,
impl<'a, T, const N: usize> Debug for ArrayChunksMut<'a, T, N> where
T: 'a + Debug,
impl<'a, T, const N: usize> Debug for ArrayWindows<'a, T, N> where
T: 'a + Debug,
impl<'a, T: Debug + 'a> Debug for alloc::collections::binary_heap::Drain<'a, T>
impl<'a, T: Debug + 'a> Debug for alloc::collections::btree_set::Range<'a, T>
impl<'a, T: Debug + Ord> Debug for DrainSorted<'a, T>
impl<'a, T: Debug, F: Debug, A: Debug + Allocator> Debug for alloc::vec::DrainFilter<'a, T, F, A> where
F: FnMut(&mut T) -> bool,
impl<'a, const N: usize> Debug for CharArraySearcher<'a, N>
impl<'f> Debug for VaListImpl<'f>
impl<A> Debug for Repeat<A> where
A: Debug,
impl<A> Debug for core::option::IntoIter<A> where
A: Debug,
impl<A, B> Debug for Chain<A, B> where
A: Debug,
B: Debug,
impl<A, B> Debug for Zip<A, B> where
A: Debug,
B: Debug,
impl<B, C> Debug for ControlFlow<B, C> where
B: Debug,
C: Debug,
impl<B: ?Sized> Debug for Cow<'_, B> where
B: Debug + ToOwned<Owned: Debug>,
impl<Dyn> Debug for DynMetadata<Dyn> where
Dyn: ?Sized,
impl<F> Debug for PollFn<F>
impl<F> Debug for FromFn<F>
impl<F> Debug for OnceWith<F> where
F: Debug,
impl<F> Debug for RepeatWith<F> where
F: Debug,
impl<F> Debug for CharPredicateSearcher<'_, F> where
F: FnMut(char) -> bool,
impl<H> Debug for BuildHasherDefault<H>
impl<I> Debug for FromIter<I> where
I: Debug,
impl<I> Debug for DecodeUtf16<I> where
I: Debug + Iterator<Item = u16>,
impl<I> Debug for Cloned<I> where
I: Debug,
impl<I> Debug for Copied<I> where
I: Debug,
impl<I> Debug for Cycle<I> where
I: Debug,
impl<I> Debug for Enumerate<I> where
I: Debug,
impl<I> Debug for Fuse<I> where
I: Debug,
impl<I> Debug for Intersperse<I> where
I: Debug + Iterator,
<I as Iterator>::Item: Clone,
<I as Iterator>::Item: Debug,
impl<I> Debug for Peekable<I> where
I: Debug + Iterator,
<I as Iterator>::Item: Debug,
impl<I> Debug for Skip<I> where
I: Debug,
impl<I> Debug for StepBy<I> where
I: Debug,
impl<I> Debug for Take<I> where
I: Debug,
impl<I, F> Debug for FilterMap<I, F> where
I: Debug,
impl<I, F> Debug for Inspect<I, F> where
I: Debug,
impl<I, F> Debug for Map<I, F> where
I: Debug,
impl<I, G> Debug for IntersperseWith<I, G> where
I: Iterator + Debug,
G: Debug,
<I as Iterator>::Item: Debug,
impl<I, P> Debug for Filter<I, P> where
I: Debug,
impl<I, P> Debug for MapWhile<I, P> where
I: Debug,
impl<I, P> Debug for SkipWhile<I, P> where
I: Debug,
impl<I, P> Debug for TakeWhile<I, P> where
I: Debug,
impl<I, St, F> Debug for Scan<I, St, F> where
I: Debug,
St: Debug,
impl<I, U> Debug for Flatten<I> where
I: Debug + Iterator,
U: Debug + Iterator,
<I as Iterator>::Item: IntoIterator,
<<I as Iterator>::Item as IntoIterator>::IntoIter == U,
<<I as Iterator>::Item as IntoIterator>::Item == <U as Iterator>::Item,
impl<I, U, F> Debug for FlatMap<I, U, F> where
I: Debug,
U: IntoIterator,
<U as IntoIterator>::IntoIter: Debug,
impl<I, const N: usize> Debug for core::iter::adapters::array_chunks::ArrayChunks<I, N> where
I: Debug + Iterator,
<I as Iterator>::Item: Debug,
impl<Idx> Debug for core::ops::range::Range<Idx> where
Idx: Debug,
impl<Idx> Debug for RangeFrom<Idx> where
Idx: Debug,
impl<Idx> Debug for RangeInclusive<Idx> where
Idx: Debug,
impl<Idx> Debug for RangeTo<Idx> where
Idx: Debug,
impl<Idx> Debug for RangeToInclusive<Idx> where
Idx: Debug,
impl<K, V, F> Debug for alloc::collections::btree_map::DrainFilter<'_, K, V, F> where
K: Debug,
V: Debug,
F: FnMut(&K, &mut V) -> bool,
impl<K, V: Debug> Debug for Values<'_, K, V>
impl<K, V: Debug> Debug for ValuesMut<'_, K, V>
impl<K, V: Debug, A: Allocator + Clone> Debug for IntoValues<K, V, A>
impl<K: Debug + Ord, V, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, A>
impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for Entry<'_, K, V, A>
impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for OccupiedEntry<'_, K, V, A>
impl<K: Debug + Ord, V: Debug, A: Allocator + Clone> Debug for OccupiedError<'_, K, V, A>
impl<K: Debug, V> Debug for Keys<'_, K, V>
impl<K: Debug, V, A: Allocator + Clone> Debug for IntoKeys<K, V, A>
impl<K: Debug, V: Debug> Debug for alloc::collections::btree_map::Iter<'_, K, V>
impl<K: Debug, V: Debug> Debug for alloc::collections::btree_map::IterMut<'_, K, V>
impl<K: Debug, V: Debug> Debug for alloc::collections::btree_map::Range<'_, K, V>
impl<K: Debug, V: Debug> Debug for RangeMut<'_, K, V>
impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for BTreeMap<K, V, A>
impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for alloc::collections::btree_map::IntoIter<K, V, A>
impl<P> Debug for Pin<P> where
P: Debug,
impl<Ret, T> Debug for fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> Debug for extern "C" fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> Debug for extern "C" fn (T₁, T₂, …, Tₙ, ...) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> Debug for unsafe fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> Debug for unsafe extern "C" fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<Ret, T> Debug for unsafe extern "C" fn (T₁, T₂, …, Tₙ, ...) -> Ret
This trait is implemented for function pointers with up to twelve arguments.
impl<T> Debug for Bound<T> where
T: Debug,
impl<T> Debug for Option<T> where
T: Debug,
impl<T> Debug for Poll<T> where
T: Debug,
impl<T> Debug for *const T where
T: ?Sized,
impl<T> Debug for *mut T where
T: ?Sized,
impl<T> Debug for &T where
T: Debug + ?Sized,
impl<T> Debug for &mut T where
T: Debug + ?Sized,
impl<T> Debug for [T] where
T: Debug,
impl<T> Debug for (T₁, T₂, …, Tₙ) where
T: Debug + ?Sized,
This trait is implemented for tuples up to twelve items long.