Trait std::fmt::Debug [] [src]

pub trait Debug {
    fn fmt(&self, &mut Formatter) -> Result<(), Error>;
}

Format trait for the ? character.

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].

Examples

Deriving an implementation:

fn main() { #[derive(Debug)] struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:?}", origin); }
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

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

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

Manually implementing:

fn main() { 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); }
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 #?:

fn main() { #[derive(Debug)] struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:#?}", origin); }
#[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

fn fmt(&self, &mut Formatter) -> Result<(), Error>

Formats the value using the given formatter.

Implementors