mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-08-12 06:24:44 +00:00
update repo layout
This commit is contained in:
153
en/src/formatted-output/debug-display.md
Normal file
153
en/src/formatted-output/debug-display.md
Normal file
@@ -0,0 +1,153 @@
|
||||
# Debug and Display
|
||||
All types which want to be printable must implement the `std::fmt` formatting trait: `std::fmt::Debug` or `std::fmt::Display`.
|
||||
|
||||
Automatic implementations are only provided for types such as in the `std` library. All others have to be manually implemented.
|
||||
|
||||
## Debug
|
||||
The implementation of `Debug` is very straightfoward: All types can `derive` the `std::fmt::Debug` implementation. This is not true for `std::fmt::Display` which must be manually implemented.
|
||||
|
||||
`{:?}` must be used to print out the type which has implemented the `Debug` trait.
|
||||
|
||||
```rust
|
||||
// This structure cannot be printed either with `fmt::Display` or
|
||||
// with `fmt::Debug`.
|
||||
struct UnPrintable(i32);
|
||||
|
||||
// To make this struct printable with `fmt::Debug`, we can derive the automatic implementations provided by Rust
|
||||
#[derive(Debug)]
|
||||
struct DebugPrintable(i32);
|
||||
```
|
||||
|
||||
1. 🌟
|
||||
```rust,editable
|
||||
|
||||
/* Fill in the blanks and Fix the errors */
|
||||
struct Structure(i32);
|
||||
|
||||
fn main() {
|
||||
// Types in std and Rust have implemented the fmt::Debug trait
|
||||
println!("__ months in a year.", 12);
|
||||
|
||||
println!("Now __ will print!", Structure(3));
|
||||
}
|
||||
```
|
||||
|
||||
2. 🌟🌟 So `fmt::Debug` definitely makes one type printable, but sacrifices some elegance. Maybe we can get more elegant by replacing `{:?}` with something else( but not `{}` !)
|
||||
```rust,editable
|
||||
#[derive(Debug)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u8
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let person = Person { name: "Sunface".to_string(), age: 18 };
|
||||
|
||||
/* Make it output:
|
||||
Person {
|
||||
name: "Sunface",
|
||||
age: 18,
|
||||
}
|
||||
*/
|
||||
println!("{:?}", person);
|
||||
}
|
||||
```
|
||||
|
||||
3. 🌟🌟 We can also manually implement `Debug` trait for our types
|
||||
```rust,editable
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Structure(i32);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Deep(Structure);
|
||||
|
||||
|
||||
fn main() {
|
||||
// The problem with `derive` is there is no control over how
|
||||
// the results look. What if I want this to just show a `7`?
|
||||
|
||||
/* Make it print: Now 7 will print! */
|
||||
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||
}
|
||||
```
|
||||
|
||||
## Display
|
||||
Yeah, `Debug` is simple and easy to use. But sometimes we want to customize the output appearance of our type. This is where `Display` really shines.
|
||||
|
||||
Unlike `Debug`, there is no way to derive the implementation of the `Display` trait, we have to manually implement it.
|
||||
|
||||
Anotherthing to note: the placefolder for `Display` is `{}` not `{:?}`.
|
||||
|
||||
4. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
/* Make it work*/
|
||||
use std::fmt;
|
||||
|
||||
struct Point2D {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl fmt::Display for Point2D {
|
||||
/* Implement.. */
|
||||
}
|
||||
|
||||
impl fmt::Debug for Point2D {
|
||||
/* Implement.. */
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let point = Point2D { x: 3.3, y: 7.2 };
|
||||
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
|
||||
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }");
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### `?` operator
|
||||
|
||||
Implementing `fmt::Display` for a structure whose elements must be handled separately is triky. The problem is each `write!` generates a `fmt::Result` which must be handled in the same place.
|
||||
|
||||
Fortunately, Rust provides the `?` operator to help us eliminate some unnecessary codes for deaing with `fmt::Result`.
|
||||
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
/* Make it work */
|
||||
use std::fmt;
|
||||
|
||||
struct List(Vec<i32>);
|
||||
|
||||
impl fmt::Display for List {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// Extract the value using tuple indexing,
|
||||
// and create a reference to `vec`.
|
||||
let vec = &self.0;
|
||||
|
||||
write!(f, "[")?;
|
||||
|
||||
// Iterate over `v` in `vec` while enumerating the iteration
|
||||
// count in `count`.
|
||||
for (count, v) in vec.iter().enumerate() {
|
||||
// For every element except the first, add a comma.
|
||||
// Use the ? operator to return on errors.
|
||||
if count != 0 { write!(f, ", ")?; }
|
||||
write!(f, "{}", v)?;
|
||||
}
|
||||
|
||||
// Close the opened bracket and return a fmt::Result value.
|
||||
write!(f, "]")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v = List(vec![1, 2, 3]);
|
||||
assert_eq!(format!("{}",v), "[0: 1, 1: 2, 2: 3]");
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
183
en/src/formatted-output/formatting.md
Normal file
183
en/src/formatted-output/formatting.md
Normal file
@@ -0,0 +1,183 @@
|
||||
# formating
|
||||
|
||||
## Positional arguments
|
||||
|
||||
1.🌟🌟
|
||||
```rust,edtiable
|
||||
|
||||
/* Fill in the blanks */
|
||||
fn main() {
|
||||
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");// => Alice, this is Bob. Bob, this is Alice
|
||||
assert_eq!(format!("{1}{0}", 1, 2), __);
|
||||
assert_eq!(format!(__, 1, 2), "2112");
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
## Named arguments
|
||||
|
||||
2.🌟🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
println!("{argument}", argument = "test"); // => "test"
|
||||
|
||||
/* Fill in the blanks */
|
||||
assert_eq!(format!("{name}{}", 1, __), "21");
|
||||
assert_eq!(format!(__,a = "a", b = 'b', c = 3 ), "a 3 b");
|
||||
|
||||
/* Fix the error */
|
||||
// named argument must be placed after other arguments
|
||||
println!("{abc} {1}", abc = "def", 2);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
## Padding with string
|
||||
|
||||
3.🌟🌟 By default, you can pad string with spaces
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// the following two are padding with 5 spaces
|
||||
println!("Hello {:5}!", "x"); // => "Hello x !"
|
||||
println!("Hello {:1$}!", "x", 5); // => "Hello x !"
|
||||
|
||||
/* Fill in the blanks */
|
||||
assert_eq!(format!("Hello __!", 5, "x"), "Hello x !");
|
||||
assert_eq!(format!("Hello __!", "x", width = 5), "Hello x !");
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
4.🌟🌟🌟 Left align, right align, pad with specified characters.
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// left align
|
||||
println!("Hello {:<5}!", "x"); // => Hello x !
|
||||
// right align
|
||||
assert_eq!(format!("Hello __!", "x"), "Hello x!");
|
||||
// center align
|
||||
assert_eq!(format!("Hello __!", "x"), "Hello x !");
|
||||
|
||||
// left align, pad with '&'
|
||||
assert_eq!(format!("Hello {:&<5}!", "x"), __);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
5.🌟🌟 You can pad numbers with extra zeros.
|
||||
```rust,editable
|
||||
fn main() {
|
||||
println!("Hello {:5}!", 5); // => Hello 5!
|
||||
println!("Hello {:+}!", 5); // => Hello +5!
|
||||
println!("Hello {:05}!", 5); // => Hello 00005!
|
||||
println!("Hello {:05}!", -5); // => Hello -0005!
|
||||
|
||||
/* Fill in the blank */
|
||||
assert!(format!("{number:0>width$}", number=1, width=6) == __);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
## precision
|
||||
6.🌟🌟 Floating point precision
|
||||
```rust,editable
|
||||
|
||||
/* Fill in the blanks */
|
||||
fn main() {
|
||||
let v = 3.1415926;
|
||||
|
||||
println!("{:.1$}", v, 4); // same as {:.4} => 3.1416
|
||||
|
||||
assert_eq!(format!("__", v), "3.14");
|
||||
assert_eq!(format!("__", v), "+3.14");
|
||||
assert_eq!(format!("__", v), "3");
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
7.🌟🌟🌟 string length
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let s = "Hello, world!";
|
||||
|
||||
println!("{0:.5}", s); // => Hello
|
||||
|
||||
assert_eq!(format!("Hello __!", 3, "abcdefg"), "Hello abc!");
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
## binary, octal, hex
|
||||
|
||||
- format!("{}", foo) -> "3735928559"
|
||||
- format!("0x{:X}", foo) -> "0xDEADBEEF"
|
||||
- format!("0o{:o}", foo) -> "0o33653337357"
|
||||
|
||||
8.🌟🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
assert_eq!(format!("__", 27), "0b11011");
|
||||
assert_eq!(format!("__", 27), "0o33");
|
||||
assert_eq!(format!("__", 27), "0x1b");
|
||||
assert_eq!(format!("__", 27), "0x1B");
|
||||
|
||||
println!("{:x}!", 27); // hex with no prefix => 1b
|
||||
|
||||
println!("{:#010b}", 27); // pad binary with 0, width = 10, => 0b00011011
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
## Capture the enviroments
|
||||
9.🌟🌟🌟
|
||||
```rust,editable
|
||||
fn get_person() -> String {
|
||||
String::from("sunface")
|
||||
}
|
||||
|
||||
fn get_format() -> (usize, usize) {
|
||||
(4, 1)
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let person = get_person();
|
||||
println!("Hello, {person}!");
|
||||
|
||||
let (width, precision) = get_format();
|
||||
let scores = [("sunface", 99.12), ("jack", 60.34)];
|
||||
/* Make it print:
|
||||
sunface: 99.1
|
||||
jack: 60.3
|
||||
*/
|
||||
for (name, score) in scores {
|
||||
println!("{name}: __");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Others
|
||||
|
||||
**Example**
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// exponent
|
||||
println!("{:2e}", 1000000000); // => 1e9
|
||||
println!("{:2E}", 1000000000); // => 1E9
|
||||
|
||||
// pointer address
|
||||
let v= vec![1, 2, 3];
|
||||
println!("{:p}", v.as_ptr()); // => 0x600002324050
|
||||
|
||||
// escape
|
||||
println!("Hello {{}}"); // => Hello {}
|
||||
}
|
||||
```
|
66
en/src/formatted-output/intro.md
Normal file
66
en/src/formatted-output/intro.md
Normal file
@@ -0,0 +1,66 @@
|
||||
# Formatted output
|
||||
|
||||
```rust,editable,ignore,mdbook-runnable
|
||||
fn main() {
|
||||
// In general, the `{}` will be automatically replaced with any
|
||||
// arguments. These will be stringified.
|
||||
println!("{} days", 31);
|
||||
|
||||
// Without a suffix, 31 becomes an i32. You can change what type 31 is
|
||||
// by providing a suffix. The number 31i64 for example has the type i64.
|
||||
|
||||
// There are various optional patterns this works with. Positional
|
||||
// arguments can be used.
|
||||
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
|
||||
|
||||
// As can named arguments.
|
||||
println!("{subject} {verb} {object}",
|
||||
object="the lazy dog",
|
||||
subject="the quick brown fox",
|
||||
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:0>width$}", 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.
|
||||
|
||||
// For Rust 1.58 and above, you can directly capture the argument from
|
||||
// surrounding variable. Just like the above, this will output
|
||||
// " 1". 5 white spaces and a "1".
|
||||
let number: f64 = 1.0;
|
||||
let width: usize = 6;
|
||||
println!("{number:>width$}");
|
||||
}
|
||||
```
|
||||
|
||||
[`std::fmt`][fmt] contains many [`traits`][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`][string].
|
39
en/src/formatted-output/println.md
Normal file
39
en/src/formatted-output/println.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# println! and format!
|
||||
Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
|
||||
some of which include:
|
||||
|
||||
* `format!`: write formatted text to [`String`][string]
|
||||
* `print!`: same as `format!` but the text is printed to the console (io::stdout).
|
||||
* `println!`: same as `print!` but a newline is appended.
|
||||
* `eprint!`: same as `format!` but the text is printed to the standard error (io::stderr).
|
||||
* `eprintln!`: same as `eprint!`but a newline is appended.
|
||||
|
||||
All parse text in the same fashion. As a plus, Rust checks formatting
|
||||
correctness at compile time.
|
||||
|
||||
## `format!`
|
||||
1.🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let s1 = "hello";
|
||||
/* Fill in the blank */
|
||||
let s = format!(__);
|
||||
assert_eq!(s, "hello, world!");
|
||||
}
|
||||
```
|
||||
|
||||
## `print!`, `println!`
|
||||
2.🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
/* Fill in the blanks to make it print:
|
||||
Hello world, I am
|
||||
Sunface!
|
||||
*/
|
||||
__("hello world, ");
|
||||
__("I am");
|
||||
__("Sunface!");
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user