Trait alloc::fmt::Debug1.0.0[][src]

#[lang = "debug_trait"]
pub trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<(), Error>; }

? 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 ).

Examples

Deriving an implementation:

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

let origin = Point { x: 0, y: 0 };

println!("The origin is: {:?}", origin);

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 {
        write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
    }
}

let origin = Point { x: 0, y: 0 };

println!("The origin is: {:?}", origin);

This outputs:

The origin is: Point { x: 0, y: 0 }

There are a number of debug_* methods on Formatter to help you with manual implementations, such as debug_struct.

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 };

println!("The origin is: {:#?}", origin);

This outputs:

The origin is: Point {
    x: 0,
    y: 0
}

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 {
        write!(f, "({:?}, {:?})", self.longitude, self.latitude)
    }
}

assert_eq!("(1.987, 2.983)".to_owned(),
           format!("{:?}", Position { longitude: 1.987, latitude: 2.983, }));

Implementations on Foreign Types

impl Debug for RangeFull
[src]

impl<T> Debug for Empty<T>
[src]

impl<T> Debug for IntoIter<T> where
    T: Debug
[src]

impl Debug for Ordering
[src]

impl<A> Debug for Repeat<A> where
    A: Debug
[src]

impl Debug for ParseIntError
[src]

impl<'b, T> Debug for RefMut<'b, T> where
    T: Debug + ?Sized
[src]

impl<'a, T> Debug for IterMut<'a, T> where
    T: 'a + Debug
[src]

impl<T, E> Debug for Result<T, E> where
    E: Debug,
    T: Debug
[src]

impl Debug for NonZeroU32
[src]

impl Debug for TryFromIntError
[src]

impl Debug for NonZeroU8
[src]

impl<I, U, F> Debug for FlatMap<I, U, F> where
    I: Debug,
    U: IntoIterator,
    <U as IntoIterator>::IntoIter: Debug
[src]

impl Debug for SipHasher
[src]

impl<'a, A> Debug for Iter<'a, A> where
    A: 'a + Debug
[src]

impl<I> Debug for Take<I> where
    I: Debug
[src]

impl Debug for Excess
[src]

impl<I> Debug for StepBy<I> where
    I: Debug
[src]

impl Debug for Alignment
[src]

impl Debug for AtomicU8
[src]

impl<T> Debug for Discriminant<T>
[src]

impl<'b, T> Debug for Ref<'b, T> where
    T: Debug + ?Sized
[src]

impl Debug for ToLowercase
[src]

impl<'a, A> Debug for IterMut<'a, A> where
    A: 'a + Debug
[src]

impl Debug for LayoutErr
[src]

impl<I> Debug for Skip<I> where
    I: Debug
[src]

impl<I, P> Debug for SkipWhile<I, P> where
    I: Debug
[src]

impl Debug for AtomicU16
[src]

impl Debug for CollectionAllocErr
[src]

impl<I> Debug for Enumerate<I> where
    I: Debug
[src]

impl<I, P> Debug for TakeWhile<I, P> where
    I: Debug
[src]

impl<I> Debug for Fuse<I> where
    I: Debug
[src]

impl Debug for EscapeDefault
[src]

impl Debug for ToUppercase
[src]

impl<F> Debug for RepeatWith<F> where
    F: Debug
[src]

impl<T> Debug for Cell<T> where
    T: Copy + Debug
[src]

impl<T> Debug for UnsafeCell<T> where
    T: Debug + ?Sized
[src]

impl<I, F> Debug for Map<I, F> where
    I: Debug
[src]

impl<'a> Debug for Utf8LossyChunk<'a>
[src]

impl<Idx> Debug for RangeInclusive<Idx> where
    Idx: Debug
[src]

impl<Idx> Debug for RangeTo<Idx> where
    Idx: Debug
[src]

impl<I> Debug for Cloned<I> where
    I: Debug
[src]

impl<'a, T> Debug for PinMut<'a, T> where
    T: Debug + ?Sized
[src]

impl<A, B> Debug for Zip<A, B> where
    A: Debug,
    B: Debug
[src]

impl<Idx> Debug for RangeFrom<Idx> where
    Idx: Debug
[src]

impl Debug for NoneError
[src]

impl Debug for AtomicI16
[src]

impl Debug for DecodeUtf16Error
[src]

impl<'a, T> Debug for Iter<'a, T> where
    T: 'a + Debug
[src]

impl Debug for AtomicBool
[src]

impl Debug for NonZeroU16
[src]

impl Debug for Duration
[src]

impl<T> Debug for NonNull<T> where
    T: ?Sized
[src]

impl Debug for Utf8Lossy
[src]

impl<T> Debug for PhantomData<T> where
    T: ?Sized
[src]

impl<T> Debug for Rev<T> where
    T: Debug
[src]

impl Debug for AllocErr
[src]

impl Debug for AtomicI8
[src]

impl Debug for NonZeroU64
[src]

impl<'a> Debug for Location<'a>
[src]

impl Debug for TypeId
[src]

impl<Y, R> Debug for GeneratorState<Y, R> where
    R: Debug,
    Y: Debug
[src]

impl<Idx> Debug for Range<Idx> where
    Idx: Debug
[src]

impl<I, F> Debug for FilterMap<I, F> where
    I: Debug
[src]

impl Debug for AtomicU64
[src]

impl<T> Debug for Wrapping<T> where
    T: Debug
[src]

impl<Idx> Debug for RangeToInclusive<Idx> where
    Idx: Debug
[src]

impl Debug for EscapeUnicode
[src]

impl Debug for Any + 'static
[src]

impl Debug for NonZeroUsize
[src]

impl Debug for AtomicIsize
[src]

impl<A> Debug for IntoIter<A> where
    A: Debug
[src]

impl<I> Debug for Cycle<I> where
    I: Debug
[src]

impl Debug for Ordering
[src]

impl<I> Debug for DecodeUtf16<I> where
    I: Debug + Iterator<Item = u16>, 
[src]

impl Debug for Layout
[src]

impl Debug for AtomicI64
[src]

impl<T> Debug for AtomicPtr<T>
[src]

impl Debug for AtomicI32
[src]

impl<T> Debug for ManuallyDrop<T> where
    T: Debug
[src]

impl Debug for EscapeDebug
[src]

impl Debug for CannotReallocInPlace
[src]

impl<T> Debug for Option<T> where
    T: Debug
[src]

impl Debug for TryFromSliceError
[src]

impl<I, F> Debug for Inspect<I, F> where
    I: Debug
[src]

impl Debug for ParseFloatError
[src]

impl<A, B> Debug for Chain<A, B> where
    A: Debug,
    B: Debug
[src]

impl Debug for ParseCharError
[src]

impl<I, P> Debug for Filter<I, P> where
    I: Debug
[src]

impl<I> Debug for Peekable<I> where
    I: Iterator + Debug,
    <I as Iterator>::Item: Debug
[src]

impl<'a> Debug for PanicInfo<'a>
[src]

impl Debug for FpCategory
[src]

impl<T> Debug for Reverse<T> where
    T: Debug
[src]

impl<T> Debug for Once<T> where
    T: Debug
[src]

impl Debug for UnicodeVersion
[src]

impl Debug for BorrowMutError
[src]

impl Debug for EscapeDefault
[src]

impl<H> Debug for BuildHasherDefault<H>
[src]

impl<T> Debug for Bound<T> where
    T: Debug
[src]

impl Debug for Any + 'static + Send
[src]

impl Debug for InvalidSequence
[src]

impl<I, St, F> Debug for Scan<I, St, F> where
    I: Debug,
    St: Debug
[src]

impl Debug for NonZeroU128
[src]

impl<I, U> Debug for Flatten<I> where
    I: Iterator + Debug,
    U: Iterator + Debug,
    <I as Iterator>::Item: IntoIterator,
    <<I as Iterator>::Item as IntoIterator>::IntoIter == U,
    <<I as Iterator>::Item as IntoIterator>::Item == <U as Iterator>::Item
[src]

impl Debug for AtomicUsize
[src]

impl Debug for AtomicU32
[src]

impl<T> Debug for RefCell<T> where
    T: Debug + ?Sized
[src]

impl<I> Debug for DecodeUtf8<I> where
    I: Iterator<Item = u8> + Debug
[src]

impl Debug for CharTryFromError
[src]

impl Debug for BorrowError
[src]

Implementors