Выбрать главу

verb="jumps over");

// Special formatting can be specified after a `:`.

println!("{} of {:b} people know binary, the other half doesn't", 1, 2);

// You can right-align text with a specified width. This will output

// " 1". 5 white spaces and a "1".

println!("{number:>width$}", number=1, width=6);

// You can pad numbers with extra zeroes. This will output "000001".

println!("{number:>0width$}", number=1, width=6);

// Rust even checks to make sure the correct number of arguments are

// used.

println!("My name is {0}, {1} {0}", "Bond");

// FIXME ^ Add the missing argument: "James"

// Create a structure named `Structure` which contains an `i32`.

#[allow(dead_code)]

struct Structure(i32);

// However, custom types such as this structure require more complicated

// handling. This will not work.

println!("This struct `{}` won't print...", Structure(3));

// FIXME ^ Comment out this line.

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

std::fmt contains many traits which govern the display of text. The base form of two important ones are listed below:

   • fmt::Debug: Uses the {:?} marker. Format text for debugging purposes.

   • fmt::Display: Uses the {} marker. Format text in a more elegant, user friendly fashion.

Here, we used fmt::Display because the std library provides implementations for these types. To print text for custom types, more steps are required.

Implementing the fmt::Display trait automatically implements the ToString trait which allows us to convert the type to String.

   • Fix the two issues in the above code (see FIXME) so that it runs without error.

   • Add a println! macro that prints: Pi is roughly 3.142 by controlling the number of decimal places shown. For the purposes of this exercise, use let pi = 3.141592 as an estimate for pi. (Hint: you may need to check the std::fmt documentation for setting the number of decimals to display)

std::fmt, macros, struct, and traits

All types which want to use std::fmt formatting traits require an implementation to be printable. Automatic implementations are only provided for types such as in the std library. All others must be manually implemented somehow.

The fmt::Debug trait makes this very straightforward. All types can derive (automatically create) the fmt::Debug implementation. This is not true for fmt::Display which must be manually implemented.

#![allow(unused)]

fn main() {

// This structure cannot be printed either with `fmt::Display` or

// with `fmt::Debug`.

struct UnPrintable(i32);

// The `derive` attribute automatically creates the implementation

// required to make this `struct` printable with `fmt::Debug`.

#[derive(Debug)]

struct DebugPrintable(i32);

}

All std library types are automatically printable with {:?} too:

// Derive the `fmt::Debug` implementation for `Structure`. `Structure`

// is a structure which contains a single `i32`.

#[derive(Debug)]

struct Structure(i32);

// Put a `Structure` inside of the structure `Deep`. Make it printable

// also.

#[derive(Debug)]

struct Deep(Structure);

fn main() {

// Printing with `{:?}` is similar to with `{}`.

println!("{:?} months in a year.", 12);

println!("{1:?} {0:?} is the {actor:?} name.",

"Slater",

"Christian",

actor="actor's");

// `Structure` is printable!

println!("Now {:?} will print!", Structure(3));

// The problem with `derive` is there is no control over how

// the results look. What if I want this to just show a `7`?

println!("Now {:?} will print!", Deep(Structure(7)));

}

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

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

}

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