Trait core::fmt::Debug [] [src]

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

Format trait for the :? format. Useful for debugging, all types should implement this.

Generally speaking, you should just derive a Debug implementation.

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, "({}, {})", 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, "({}, {})", self.x, self.y)
    }
}

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

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

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

Required Methods

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

Formats the value using the given formatter.

Implementors