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

Trait std::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. 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 ).

Deriving an implementation:

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

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

println!("The origin is: {:?}", origin);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 { write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y) } }

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

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

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

This outputs:

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

fn [fmt](#tymethod.fmt)(&self, f: &mut [Formatter](../../std/fmt/struct.Formatter.html "struct std::fmt::Formatter")) -> [Result](../../std/result/enum.Result.html "enum std::result::Result")<[()](../primitive.unit.html), [Error](../../std/fmt/struct.Error.html "struct std::fmt::Error")>

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

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

impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [InvalidSequence](../../core/char/struct.InvalidSequence.html "struct core::char::InvalidSequence")[src]

`impl Debug for NonZero where

T: Zeroable + Debug, `[src]

impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [Alignment](../../core/fmt/enum.Alignment.html "enum core::fmt::Alignment")[src]

impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [TryFromSliceError](../../core/array/struct.TryFromSliceError.html "struct core::array::TryFromSliceError")[src]

impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [Utf8Lossy](../../std%5Funicode/lossy/struct.Utf8Lossy.html "struct std_unicode::lossy::Utf8Lossy")[src]

impl<'a> [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [Utf8LossyChunk](../../std%5Funicode/lossy/struct.Utf8LossyChunk.html "struct std_unicode::lossy::Utf8LossyChunk")<'a>[src]