Trait alloc::fmt::Debug

1.0.0 · source · []
pub trait Debug {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}
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 function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

This trait is implemented for function pointers with up to twelve arguments.

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

impl Debug for Level

impl Debug for Diagnostic

impl Debug for LexError

impl Debug for Span

impl Debug for LineColumn

impl Debug for SourceFile

impl Debug for TokenTree

impl Debug for Delimiter

impl Debug for Group

impl Debug for Spacing

impl Debug for Punct

impl Debug for Ident

impl Debug for Literal

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<E> Debug for Report<E>where
    Report<E>: Display,

impl Debug for OsString

impl Debug for OsStr

impl Debug for ReadDir

impl Debug for FileTimes

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 BorrowedBuf<'_>

impl<'a> Debug for BorrowedCursor<'a>

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 IpAddr

impl Debug for Ipv4Addr

impl Debug for Ipv6Addr

impl Debug for SocketAddr

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: Debug, F> Debug for LazyLock<T, F>

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

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

impl Debug for Once

impl Debug for OnceState

impl<T: Debug> Debug for OnceLock<T>

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<'a> Debug for CommandEnvs<'a>

impl Debug for System

impl Debug for TestOpts

impl Debug for Metric

impl Debug for Concurrent

impl Debug for RunIgnored

impl Debug for Options

impl Debug for Summary

impl Debug for TestResult

impl Debug for TestType

impl Debug for TestName

impl Debug for TestFn

impl Debug for TestId

impl Debug for TestDesc