Trait core::fmt::Debug

1.0.0 · source · []
pub trait Debug {
    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 (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 }");
Run

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 }");
Run

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)
    }
}
Run

Debug 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,
}");
Run

Required 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,
)");
Run

Implementors

This trait is implemented for tuples up to twelve items long.

impl Debug for Global

impl<T: ?Sized + Debug> Debug for ThinBox<T>

impl<T: Debug + ?Sized, A: Allocator> Debug for Box<T, A>

impl<B: ?Sized> Debug for Cow<'_, B> where
    B: Debug + ToOwned<Owned: Debug>, 

impl<T: Ord + Debug> Debug for PeekMut<'_, T>

impl<T: Debug> Debug for BinaryHeap<T>

impl<T: Debug> Debug for Iter<'_, T>

impl<T: Debug> Debug for IntoIter<T>

impl<T: Debug> Debug for IntoIterSorted<T>

impl<'a, T: Debug + 'a> Debug for Drain<'a, T>

impl<'a, T: Debug + Ord> Debug for DrainSorted<'a, T>

impl<K: Debug + Ord, V: Debug, A: Allocator> Debug for Entry<'_, K, V, A>

impl<K: Debug + Ord, V, A: Allocator> Debug for VacantEntry<'_, K, V, A>

impl<K: Debug + Ord, V: Debug, A: Allocator> Debug for OccupiedEntry<'_, K, V, A>

impl<K: Debug + Ord, V: Debug, A: Allocator> Debug for OccupiedError<'_, K, V, A>

impl<K: Debug, V: Debug> Debug for Iter<'_, K, V>

impl<K: Debug, V: Debug> Debug for IterMut<'_, K, V>

impl<K: Debug, V: Debug, A: Allocator> Debug for IntoIter<K, V, A>

impl<K: Debug, V> Debug for Keys<'_, K, V>

impl<K, V: Debug> Debug for Values<'_, K, V>

impl<K, V: Debug> Debug for ValuesMut<'_, K, V>

impl<K: Debug, V, A: Allocator> Debug for IntoKeys<K, V, A>

impl<K, V: Debug, A: Allocator> Debug for IntoValues<K, V, A>

impl<K: Debug, V: Debug> Debug for Range<'_, K, V>

impl<K: Debug, V: Debug> Debug for RangeMut<'_, K, V>

impl<K, V, F> Debug for DrainFilter<'_, K, V, F> where
    K: Debug,
    V: Debug,
    F: FnMut(&K, &mut V) -> bool

impl<K: Debug, V: Debug, A: Allocator> Debug for BTreeMap<K, V, A>

impl<T: Debug> Debug for Iter<'_, T>

impl<T: Debug, A: Debug + Allocator> Debug for IntoIter<T, A>

impl<'a, T: Debug + 'a> Debug for Range<'a, T>

impl<T: Debug, A: Allocator> Debug for Difference<'_, T, A>

impl<T: Debug> Debug for SymmetricDifference<'_, T>

impl<T: Debug, A: Allocator> Debug for Intersection<'_, T, A>

impl<T: Debug> Debug for Union<'_, T>

impl<T, F, A: Allocator> Debug for DrainFilter<'_, T, F, A> where
    T: Debug,
    F: FnMut(&T) -> bool

impl<T: Debug, A: Allocator> Debug for BTreeSet<T, A>

impl<T: Debug> Debug for Iter<'_, T>

impl<T: Debug> Debug for IterMut<'_, T>

impl<T: Debug> Debug for IntoIter<T>

impl<T: Debug> Debug for Cursor<'_, T>

impl<T: Debug> Debug for CursorMut<'_, T>

impl<T: Debug, F> Debug for DrainFilter<'_, T, F> where
    F: FnMut(&mut T) -> bool

impl<T: Debug> Debug for LinkedList<T>

impl<T: Debug, A: Allocator> Debug for Drain<'_, T, A>

impl<T: Debug> Debug for IterMut<'_, T>

impl<T: Debug, A: Allocator> Debug for IntoIter<T, A>

impl<T: Debug> Debug for Iter<'_, T>

impl<T: Debug, A: Allocator> Debug for VecDeque<T, A>

impl Debug for NulError

impl Debug for CString

impl<T: ?Sized + Debug> Debug for Rc<T>

impl<T: ?Sized + Debug> Debug for Weak<T>

impl Debug for String

impl Debug for Drain<'_>

impl<T: ?Sized + Debug> Debug for Weak<T>

impl<T: ?Sized + Debug> Debug for Arc<T>

impl<'a, T: Debug, F: Debug, A: Debug + Allocator> Debug for DrainFilter<'a, T, F, A> where
    F: FnMut(&mut T) -> bool

impl<'a, I: Debug + Iterator + 'a, A: Debug + Allocator + 'a> Debug for Splice<'a, I, A> where
    I::Item: Debug

impl<T: Debug, A: Allocator> Debug for Drain<'_, T, A>

impl<T: Debug, A: Allocator> Debug for IntoIter<T, A>

impl<T: Debug, A: Allocator> Debug for Vec<T, A>

impl<T: 'static> Debug for LocalKey<T>

impl Debug for Scope<'_, '_>

impl<'scope, T> Debug for ScopedJoinHandle<'scope, T>

impl Debug for Builder

impl Debug for ThreadId

impl Debug for Thread

impl<T> Debug for JoinHandle<T>

impl Debug for Backtrace

impl<K, V, S> Debug for HashMap<K, V, S> where
    K: Debug,
    V: Debug

impl<K: Debug, V: Debug> Debug for Iter<'_, K, V>

impl<K: Debug, V> Debug for Keys<'_, K, V>

impl<K, V: Debug> Debug for Values<'_, K, V>

impl<K, V, S> Debug for RawEntryBuilderMut<'_, K, V, S>

impl<K: Debug, V: Debug, S> Debug for RawEntryMut<'_, K, V, S>

impl<K: Debug, V: Debug, S> Debug for RawOccupiedEntryMut<'_, K, V, S>

impl<K, V, S> Debug for RawVacantEntryMut<'_, K, V, S>

impl<K, V, S> Debug for RawEntryBuilder<'_, K, V, S>

impl<K: Debug, V: Debug> Debug for Entry<'_, K, V>

impl<K: Debug, V: Debug> Debug for OccupiedEntry<'_, K, V>

impl<K: Debug, V> Debug for VacantEntry<'_, K, V>

impl<K: Debug, V: Debug> Debug for OccupiedError<'_, K, V>

impl<K, V> Debug for IterMut<'_, K, V> where
    K: Debug,
    V: Debug

impl<K: Debug, V: Debug> Debug for IntoIter<K, V>

impl<K, V: Debug> Debug for ValuesMut<'_, K, V>

impl<K: Debug, V> Debug for IntoKeys<K, V>

impl<K, V: Debug> Debug for IntoValues<K, V>

impl<K, V> Debug for Drain<'_, K, V> where
    K: Debug,
    V: Debug

impl<'a, K, V, F> Debug for DrainFilter<'a, K, V, F> where
    F: FnMut(&K, &mut V) -> bool

impl<T, S> Debug for HashSet<T, S> where
    T: Debug

impl<K: Debug> Debug for Iter<'_, K>

impl<K: Debug> Debug for IntoIter<K>

impl<K: Debug> Debug for Drain<'_, K>

impl<'a, K, F> Debug for DrainFilter<'a, K, F> where
    F: FnMut(&K) -> bool

impl<T, S> Debug for Intersection<'_, T, S> where
    T: Debug + Eq + Hash,
    S: BuildHasher

impl<T, S> Debug for Difference<'_, T, S> where
    T: Debug + Eq + Hash,
    S: BuildHasher

impl<T, S> Debug for SymmetricDifference<'_, T, S> where
    T: Debug + Eq + Hash,
    S: BuildHasher

impl<T, S> Debug for Union<'_, T, S> where
    T: Debug + Eq + Hash,
    S: BuildHasher

impl Debug for Vars

impl Debug for VarsOs

impl Debug for VarError

impl Debug for SplitPaths<'_>

impl Debug for Args

impl Debug for ArgsOs

impl<'a> Debug for Chain<'a>

impl<E> Debug for Report<E> where
    Report<E>: Display

impl Debug for OsString

impl Debug for OsStr

impl Debug for ReadDir

impl Debug for FileType

impl Debug for DirBuilder

impl Debug for File

impl Debug for Metadata

impl Debug for DirEntry

impl<R> Debug for BufReader<R> where
    R: Debug

impl<W: Write> Debug for BufWriter<W> where
    W: Debug

impl<W: Write> Debug for LineWriter<W> where
    W: Debug

impl<W: Debug> Debug for IntoInnerError<W>

impl<T: Debug> Debug for Cursor<T>

impl Debug for Error

impl Debug for ErrorKind

impl Debug for ReadBuf<'_>

impl Debug for Stdin

impl Debug for StdinLock<'_>

impl Debug for Stdout

impl Debug for StdoutLock<'_>

impl Debug for Stderr

impl Debug for StderrLock<'_>

impl Debug for Empty

impl Debug for Repeat

impl Debug for Sink

impl<'a> Debug for IoSliceMut<'a>

impl<'a> Debug for IoSlice<'a>

impl Debug for SeekFrom

impl<T: Debug, U: Debug> Debug for Chain<T, U>

impl<T: Debug> Debug for Take<T>

impl<R: Debug> Debug for Bytes<R>

impl<B: Debug> Debug for Split<B>

impl<B: Debug> Debug for Lines<B>

impl Debug for SocketAddr

impl Debug for IpAddr

impl Debug for Ipv4Addr

impl Debug for Ipv6Addr

impl<'a> Debug for Incoming<'a>

impl Debug for TcpStream

impl Debug for UdpSocket

impl Debug for Shutdown

impl Debug for SocketAddr

impl<'a> Debug for SocketAncillary<'a>

impl<'a> Debug for Incoming<'a>

impl Debug for UnixStream

impl Debug for UCred

impl Debug for PidFd

impl Debug for BorrowedHandle<'_>

impl Debug for BorrowedSocket<'_>

impl Debug for BorrowedFd<'_>

impl Debug for OwnedFd

impl<'a> Debug for Prefix<'a>

impl<'a> Debug for PrefixComponent<'a>

impl<'a> Debug for Component<'a>

impl Debug for Components<'_>

impl Debug for Iter<'_>

impl<'a> Debug for Ancestors<'a>

impl Debug for PathBuf

impl Debug for Path

impl Debug for Display<'_>

impl Debug for Child

impl Debug for ChildStdin

impl Debug for Command

impl<'a> Debug for CommandArgs<'a>

impl Debug for Output

impl Debug for Stdio

impl Debug for ExitStatus

impl Debug for ExitCode

impl<'a, T: Debug + 'a> Debug for Iter<'a, T>

impl<'a, T: Debug + 'a> Debug for TryIter<'a, T>

impl<T: Debug> Debug for IntoIter<T>

impl Debug for RecvError

impl<T> Debug for Sender<T>

impl<T> Debug for SyncSender<T>

impl<T> Debug for Receiver<T>

impl<T> Debug for SendError<T>

impl<T> Debug for TrySendError<T>

impl Debug for Barrier

impl Debug for Condvar

impl<T: ?Sized + Debug> Debug for Mutex<T>

impl<T: ?Sized + Debug> Debug for MutexGuard<'_, T>

impl Debug for OnceState

impl Debug for Once

impl<T> Debug for PoisonError<T>

impl<T> Debug for TryLockError<T>

impl<T: ?Sized + Debug> Debug for RwLock<T>

impl<T: Debug> Debug for RwLockReadGuard<'_, T>

impl<T: Debug> Debug for RwLockWriteGuard<'_, T>

impl Debug for Instant

impl Debug for SystemTime

impl<T: Debug> Debug for SyncOnceCell<T>

impl<T: Debug, F> Debug for SyncLazy<T, F>

impl<'a> Debug for CommandEnvs<'a>

impl Debug for System