Debug in std::fmt - Rust (original) (raw)

logo

Trait std::fmt::Debug

1.0.0· source · [−]

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. Whenderived 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 }. Forenums, it will use the name of the variant and, if applicable, (, then theDebug values of the fields, then ).

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.

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,debut_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

Formats the value using the given formatter.

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

impl Debug for AncillaryError

This is supported on (Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD) and Unix only.

impl Debug for PidFd

This is supported on Linux only.

impl Debug for std::os::unix:🥅:SocketAddr

This is supported on Unix only.

impl Debug for UCred

This is supported on Unix only.

impl<'_, K, V> Debug for std::collections::btree_map::Keys<'_, K, V> where

K: Debug,

impl<'_, K, V> Debug for std::collections::btree_map::Values<'_, K, V> where

V: Debug,

impl<'_, K, V, F> Debug for std::collections::btree_map::DrainFilter<'_, K, V, F> where

K: Debug,
V: Debug,
F: FnMut(&K, &mut V) -> bool,

impl<'_, T> Debug for std::collections::binary_heap::Iter<'_, T> where

T: Debug,

impl<'_, T> Debug for std::collections::btree_set::Iter<'_, T> where

T: Debug,

impl<'_, T> Debug for std::collections::btree_set::Union<'_, T> where

T: Debug,

impl<'_, T> Debug for std::collections::linked_list::Cursor<'_, T> where

T: Debug,

impl<'_, T> Debug for std::collections::linked_list::Iter<'_, T> where

T: Debug,

impl<'_, T> Debug for std::collections::vec_deque::Iter<'_, T> where

T: Debug,

impl<'a> Debug for std::os::unix:🥅:Incoming<'a>

This is supported on Unix only.

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

This is supported on (Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD) and Unix only.

impl<'a, T> Debug for std::collections::binary_heap::Drain<'a, T> where

T: 'a + Debug,

impl<'a, T> Debug for std::collections::btree_set::Range<'a, T> where

T: 'a + Debug,

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

impl Debug for extern "C" fn() -> Ret

impl Debug for unsafe fn() -> Ret

impl Debug for unsafe extern "C" fn() -> Ret

impl<Ret, A> Debug for fn(A) -> Ret

impl<Ret, A> Debug for extern "C" fn(A) -> Ret

impl<Ret, A> Debug for extern "C" fn(A, ...) -> Ret

impl<Ret, A> Debug for unsafe fn(A) -> Ret

impl<Ret, A> Debug for unsafe extern "C" fn(A) -> Ret

impl<Ret, A> Debug for unsafe extern "C" fn(A, ...) -> Ret

impl<Ret, A, B> Debug for fn(A, B) -> Ret

impl<Ret, A, B> Debug for extern "C" fn(A, B) -> Ret

impl<Ret, A, B> Debug for extern "C" fn(A, B, ...) -> Ret

impl<Ret, A, B> Debug for unsafe fn(A, B) -> Ret

impl<Ret, A, B> Debug for unsafe extern "C" fn(A, B) -> Ret

impl<Ret, A, B> Debug for unsafe extern "C" fn(A, B, ...) -> Ret

impl<Ret, A, B, C> Debug for fn(A, B, C) -> Ret

impl<Ret, A, B, C> Debug for extern "C" fn(A, B, C) -> Ret

impl<Ret, A, B, C> Debug for extern "C" fn(A, B, C, ...) -> Ret

impl<Ret, A, B, C> Debug for unsafe fn(A, B, C) -> Ret

impl<Ret, A, B, C> Debug for unsafe extern "C" fn(A, B, C) -> Ret

impl<Ret, A, B, C> Debug for unsafe extern "C" fn(A, B, C, ...) -> Ret

impl<Ret, A, B, C, D> Debug for fn(A, B, C, D) -> Ret

impl<Ret, A, B, C, D> Debug for extern "C" fn(A, B, C, D) -> Ret

impl<Ret, A, B, C, D> Debug for extern "C" fn(A, B, C, D, ...) -> Ret

impl<Ret, A, B, C, D> Debug for unsafe fn(A, B, C, D) -> Ret

impl<Ret, A, B, C, D> Debug for unsafe extern "C" fn(A, B, C, D) -> Ret

impl<Ret, A, B, C, D> Debug for unsafe extern "C" fn(A, B, C, D, ...) -> Ret

impl<Ret, A, B, C, D, E> Debug for fn(A, B, C, D, E) -> Ret

impl<Ret, A, B, C, D, E> Debug for extern "C" fn(A, B, C, D, E) -> Ret

impl<Ret, A, B, C, D, E> Debug for extern "C" fn(A, B, C, D, E, ...) -> Ret

impl<Ret, A, B, C, D, E> Debug for unsafe fn(A, B, C, D, E) -> Ret

impl<Ret, A, B, C, D, E> Debug for unsafe extern "C" fn(A, B, C, D, E) -> Ret

impl<Ret, A, B, C, D, E> Debug for unsafe extern "C" fn(A, B, C, D, E, ...) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for fn(A, B, C, D, E, F) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for extern "C" fn(A, B, C, D, E, F) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for extern "C" fn(A, B, C, D, E, F, ...) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for unsafe fn(A, B, C, D, E, F) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for unsafe extern "C" fn(A, B, C, D, E, F) -> Ret

impl<Ret, A, B, C, D, E, F> Debug for unsafe extern "C" fn(A, B, C, D, E, F, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for fn(A, B, C, D, E, F, G) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for extern "C" fn(A, B, C, D, E, F, G) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for extern "C" fn(A, B, C, D, E, F, G, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for unsafe fn(A, B, C, D, E, F, G) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G) -> Ret

impl<Ret, A, B, C, D, E, F, G> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for fn(A, B, C, D, E, F, G, H) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for extern "C" fn(A, B, C, D, E, F, G, H) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for extern "C" fn(A, B, C, D, E, F, G, H, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe fn(A, B, C, D, E, F, G, H) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H) -> Ret

impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for fn(A, B, C, D, E, F, G, H, I) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe fn(A, B, C, D, E, F, G, H, I) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for fn(A, B, C, D, E, F, G, H, I, J) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L, ...) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret

impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L, ...) -> Ret

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where

T0: Debug,
T1: Debug,
T2: Debug,
T3: Debug,
T4: Debug,
T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where

T1: Debug,
T2: Debug,
T3: Debug,
T4: Debug,
T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where

T2: Debug,
T3: Debug,
T4: Debug,
T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T3, T4, T5, T6, T7, T8, T9, T10, T11) where

T3: Debug,
T4: Debug,
T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T4, T5, T6, T7, T8, T9, T10, T11) where

T4: Debug,
T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T5, T6, T7, T8, T9, T10, T11> Debug for (T5, T6, T7, T8, T9, T10, T11) where

T5: Debug,
T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T6, T7, T8, T9, T10, T11> Debug for (T6, T7, T8, T9, T10, T11) where

T6: Debug,
T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T7, T8, T9, T10, T11> Debug for (T7, T8, T9, T10, T11) where

T7: Debug,
T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,

impl<T8, T9, T10, T11> Debug for (T8, T9, T10, T11) where

T8: Debug,
T9: Debug,
T10: Debug,
T11: Debug + ?Sized,