Читаем Rust by Example полностью

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

So fmt::Debug definitely makes this printable but sacrifices some elegance. Rust also provides "pretty printing" with {:#?}.

#[derive(Debug)]

struct Person<'a> {

name: &'a str,

age: u8

}

fn main() {

let name = "Peter";

let age = 27;

let peter = Person { name, age };

// Pretty print

println!("{:#?}", peter);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

One can manually implement fmt::Display to control the display.

<p id="see_also_2"><strong><a l:href="#see_also_2">See also:</a></strong></p>

attributes, derive, std::fmt, and struct

<p id="display"><strong><a l:href="#display">Display</a></strong></p>

fmt::Debug hardly looks compact and clean, so it is often advantageous to customize the output appearance. This is done by manually implementing fmt::Display, which uses the {} print marker. Implementing it looks like this:

#![allow(unused)]

fn main() {

// Import (via `use`) the `fmt` module to make it available.

use std::fmt;

// Define a structure for which `fmt::Display` will be implemented. This is

// a tuple struct named `Structure` that contains an `i32`.

struct Structure(i32);

// To use the `{}` marker, the trait `fmt::Display` must be implemented

// manually for the type.

impl fmt::Display for Structure {

// This trait requires `fmt` with this exact signature.

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

// Write strictly the first element into the supplied output

// stream: `f`. Returns `fmt::Result` which indicates whether the

// operation succeeded or failed. Note that `write!` uses syntax which

// is very similar to `println!`.

write!(f, "{}", self.0)

}

}

}

fmt::Display may be cleaner than fmt::Debug but this presents a problem for the std library. How should ambiguous types be displayed? For example, if the std library implemented a single style for all Vec, what style should it be? Would it be either of these two?

   • Vec: /:/etc:/home/username:/bin (split on :)

   • Vec: 1,2,3 (split on ,)

Перейти на страницу:

Похожие книги