pub trait Debug {
// Required method
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
Expand description
?
格式。
Debug
应该在面向程序员的调试上下文中格式化输出。
一般来说,您应该只将 derive
和 Debug
实现。
当与备用格式说明符 #?
一起使用时,输出将被漂亮地打印。
有关格式化程序的更多信息,请参见 模块级文档。
如果所有字段都实现 Debug
,则此 trait 可以与 #[derive]
一起使用。当为结构体 derived' 时,它将使用
struct的名称,然后是
{,然后是每个字段名称和
Debug值的逗号分隔列表,然后是
}。 对于
enum,它将使用变体的名称,如果适用,将使用
(,然后是字段的
Debug值,然后是
)`。
Stability
派生的 Debug
格式不稳定,因此 future Rust 版本可能会更改。此外,标准库 (std
、core
、alloc
等) 提供的类型的 Debug
实现并不稳定,并且也可能随着 future Rust 版本而改变。
Examples
派生实现:
#[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 }");
Run手动实现:
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 }");
RunFormatter
结构体上有许多辅助方法可以帮助您实现手动实现,例如 debug_struct
。
不希望使用 Formatter
trait 提供的标准调试表示套件 (debug_struct
、debug_tuple
、debug_list
、debug_set
、debug_map
) 的类型可以通过手动向 Formatter
写入任意表示来完成完全自定义的操作。
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Point [{} {}]", self.x, self.y)
}
}
Run使用 derive
或 Formatter
上的调试构建器 API 的 Debug
实现支持使用备用标志 {:#?}
的漂亮打印。
使用 #?
进行漂亮的打印:
#[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§
sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
使用给定的格式化程序格式化该值。
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 AsciiChar
impl Debug for core::cmp::Ordering
impl Debug for Infallible
impl Debug for c_void
impl Debug for IpAddr
impl Debug for Ipv6MulticastScope
impl Debug for SocketAddr
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 core::fmt::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
Available on AArch64 only.
impl Debug for float64x1x2_t
Available on AArch64 only.
impl Debug for float64x1x3_t
Available on AArch64 only.
impl Debug for float64x1x4_t
Available on AArch64 only.
impl Debug for float64x2_t
Available on AArch64 only.
impl Debug for float64x2x2_t
Available on AArch64 only.
impl Debug for float64x2x3_t
Available on AArch64 only.
impl Debug for float64x2x4_t
Available on AArch64 only.
impl Debug for int16x2_t
Available on ARM only.
impl Debug for uint16x2_t
Available on ARM only.
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
Available on ARM only.
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
Available on ARM only.
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_char
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_bool_int
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_bool_long
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_bool_short
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_double
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_float
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_signed_char
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_signed_int
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_signed_long
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_signed_short
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_unsigned_char
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_unsigned_int
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_unsigned_long
Available on PowerPC or PowerPC-64 only.
impl Debug for vector_unsigned_short
Available on PowerPC or PowerPC-64 only.
impl Debug for v128
Available on
target_family="wasm"
only.impl Debug for CpuidResult
Available on x86 or x86-64 only.
impl Debug for __m128
Available on x86 or x86-64 only.
impl Debug for __m128bh
Available on x86 or x86-64 only.
impl Debug for __m128d
Available on x86 or x86-64 only.
impl Debug for __m128i
Available on x86 or x86-64 only.
impl Debug for __m256
Available on x86 or x86-64 only.
impl Debug for __m256bh
Available on x86 or x86-64 only.
impl Debug for __m256d
Available on x86 or x86-64 only.
impl Debug for __m256i
Available on x86 or x86-64 only.
impl Debug for __m512
Available on x86 or x86-64 only.
impl Debug for __m512bh
Available on x86 or x86-64 only.
impl Debug for __m512d
Available on x86 or x86-64 only.
impl Debug for __m512i
Available on x86 or x86-64 only.
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 DecodeUtf16Error
impl Debug for core::char::EscapeDebug
impl Debug for core::char::EscapeDefault
impl Debug for core::char::EscapeUnicode
impl Debug for ParseCharError
impl Debug for ToLowercase
impl Debug for ToUppercase
impl Debug for TryFromCharError
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 AddrParseError
impl Debug for Ipv4Addr
impl Debug for Ipv6Addr
impl Debug for SocketAddrV4
impl Debug for SocketAddrV6
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 ParseFloatError
impl Debug for ParseIntError
impl Debug for TryFromIntError
impl Debug for RangeFull
impl Debug for core::ptr::Alignment
impl Debug for TimSortRun
impl Debug for Chars<'_>
impl Debug for EncodeUtf16<'_>
impl Debug for ParseBoolError
impl Debug for Utf8Chunks<'_>
impl Debug for Utf8Error
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 TryFromFloatSecsError
impl Debug for Arguments<'_>
impl Debug for Error
impl Debug for dyn Any
impl Debug for dyn Any + Send
impl Debug for dyn Any + Send + Sync
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 core::str::EscapeDebug<'a>
impl<'a> Debug for core::str::EscapeDefault<'a>
impl<'a> Debug for core::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: 'a> Debug for VaList<'a, 'f>
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, I: Debug> Debug for ByRefSized<'a, I>
impl<'a, P> Debug for MatchIndices<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for Matches<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for RMatchIndices<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for RMatches<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for core::str::RSplit<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for core::str::RSplitN<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for RSplitTerminator<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for core::str::Split<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for core::str::SplitN<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P> Debug for SplitTerminator<'a, P>where P: Pattern<'a, Searcher: Debug>,
impl<'a, P: Pattern<'a, Searcher: Debug>> Debug for core::str::SplitInclusive<'a, P>
impl<'a, T: 'a + Debug, P> Debug for GroupBy<'a, T, P>
impl<'a, T: 'a + Debug, P> Debug for GroupByMut<'a, T, P>
impl<'a, T: Debug + 'a> Debug for core::result::Iter<'a, T>
impl<'a, T: Debug + 'a> Debug for core::result::IterMut<'a, T>
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 RChunks<'a, T>
impl<'a, T: Debug + 'a> Debug for RChunksExact<'a, T>
impl<'a, T: Debug + 'a> Debug for RChunksExactMut<'a, T>
impl<'a, T: Debug + 'a> Debug for RChunksMut<'a, T>
impl<'a, T: Debug + 'a> Debug for Windows<'a, T>
impl<'a, T: Debug + 'a, const N: usize> Debug for core::slice::ArrayChunks<'a, T, N>
impl<'a, T: Debug + 'a, const N: usize> Debug for ArrayChunksMut<'a, T, N>
impl<'a, T: Debug + 'a, const N: usize> Debug for ArrayWindows<'a, T, N>
impl<'a, const N: usize> Debug for CharArraySearcher<'a, N>
impl<'f> Debug for VaListImpl<'f>
impl<A: Debug> Debug for Repeat<A>
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: ?Sized> Debug for DynMetadata<Dyn>
impl<F> Debug for PollFn<F>
impl<F> Debug for FromFn<F>
impl<F> Debug for OnceWith<F>
impl<F> Debug for RepeatWith<F>
impl<F> Debug for CharPredicateSearcher<'_, F>where F: FnMut(char) -> bool,
impl<F: FnPtr> Debug for F
impl<H> Debug for BuildHasherDefault<H>
impl<I> Debug for DecodeUtf16<I>where I: Iterator<Item = u16> + Debug,
impl<I, G> Debug for IntersperseWith<I, G>where I: Iterator + Debug, I::Item: Debug, G: Debug,
impl<I, U> Debug for Flatten<I>where I: Debug + Iterator<Item: IntoIterator<IntoIter = U, Item = U::Item>>, U: Debug + Iterator,
impl<I: Debug + Iterator> Debug for Intersperse<I>where I::Item: Clone + Debug,
impl<I: Debug + Iterator> Debug for Peekable<I>where I::Item: Debug,
impl<I: Debug + Iterator, const N: usize> Debug for core::iter::ArrayChunks<I, N>where I::Item: Debug,
impl<I: Debug> Debug for FromIter<I>
impl<I: Debug> Debug for Cloned<I>
impl<I: Debug> Debug for Copied<I>
impl<I: Debug> Debug for Cycle<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 FilterMap<I, F>
impl<I: Debug, F> Debug for Inspect<I, F>
impl<I: Debug, F> Debug for Map<I, F>
impl<I: Debug, P> Debug for Filter<I, P>
impl<I: Debug, P> Debug for MapWhile<I, P>
impl<I: Debug, P> Debug for SkipWhile<I, P>
impl<I: Debug, P> Debug for TakeWhile<I, P>
impl<I: Debug, St: Debug, F> Debug for Scan<I, St, F>
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> Debug for Pin<P>
impl<T> Debug for (T₁, T₂, …, Tₙ)where T: ?Sized + Debug,
This trait is implemented for tuples up to twelve items long.