commit
080495bd2f
|
@ -77,7 +77,7 @@ Yeah, `Debug` is simple and easy to use. But sometimes we want to customize the
|
|||
|
||||
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 `{:?}`.
|
||||
Another thing to note: the placefolder for `Display` is `{}` not `{:?}`.
|
||||
|
||||
4. ππ
|
||||
```rust,editable
|
||||
|
@ -103,16 +103,16 @@ fn main() {
|
|||
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
|
||||
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }");
|
||||
|
||||
println!("Success!")
|
||||
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.
|
||||
Implementing `fmt::Display` for a structure whose elements must be handled separately is tricky. 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`.
|
||||
Fortunately, Rust provides the `?` operator to help us eliminate some unnecessary codes for dealing with `fmt::Result`.
|
||||
|
||||
5. ππ
|
||||
```rust,editable
|
||||
|
@ -147,7 +147,7 @@ impl fmt::Display for List {
|
|||
fn main() {
|
||||
let v = List(vec![1, 2, 3]);
|
||||
assert_eq!(format!("{}",v), "[0: 1, 1: 2, 2: 3]");
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# formating
|
||||
# Formatting
|
||||
|
||||
## Positional arguments
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
|||
```rust,editable
|
||||
/* 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
|
||||
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!");
|
||||
|
@ -25,10 +25,10 @@ fn main() {
|
|||
assert_eq!(format!(__,a = "a", b = 'b', c = 3 ), "a 3 b");
|
||||
|
||||
/* Fix the error */
|
||||
// named argument must be placed after other arguments
|
||||
// Named argument must be placed after other arguments
|
||||
println!("{abc} {1}", abc = "def", 2);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -37,7 +37,7 @@ fn main() {
|
|||
3.ππ By default, you can pad string with spaces
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// the following two are padding with 5 spaces
|
||||
// The following two are padding with 5 spaces
|
||||
println!("Hello {:5}!", "x"); // => "Hello x !"
|
||||
println!("Hello {:1$}!", "x", 5); // => "Hello x !"
|
||||
|
||||
|
@ -45,24 +45,24 @@ fn main() {
|
|||
assert_eq!(format!("Hello __!", 5, "x"), "Hello x !");
|
||||
assert_eq!(format!("Hello __!", "x", width = 5), "Hello x !");
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
4.πππ Left align, right align, pad with specified characters.
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// left align
|
||||
// Left align
|
||||
println!("Hello {:<5}!", "x"); // => Hello x !
|
||||
// right align
|
||||
// Right align
|
||||
assert_eq!(format!("Hello __!", "x"), "Hello x!");
|
||||
// center align
|
||||
// Center align
|
||||
assert_eq!(format!("Hello __!", "x"), "Hello x !");
|
||||
|
||||
// left align, pad with '&'
|
||||
// Left align, pad with '&'
|
||||
assert_eq!(format!("Hello {:&<5}!", "x"), __);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -78,10 +78,10 @@ fn main() {
|
|||
assert!(format!("{number:0>width$}", number=1, width=6) == __);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
;}
|
||||
```
|
||||
|
||||
## precision
|
||||
## Precision
|
||||
6.ππ Floating point precision
|
||||
```rust,editable
|
||||
|
||||
|
@ -95,11 +95,11 @@ fn main() {
|
|||
assert_eq!(format!("__", v), "+3.14");
|
||||
assert_eq!(format!("__", v), "3");
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
7.πππ string length
|
||||
7.πππ String length
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let s = "Hello, world!";
|
||||
|
@ -108,11 +108,11 @@ fn main() {
|
|||
|
||||
assert_eq!(format!("Hello __!", 3, "abcdefg"), "Hello abc!");
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
## binary, octal, hex
|
||||
## Binary, octal, hex
|
||||
|
||||
- format!("{}", foo) -> "3735928559"
|
||||
- format!("0x{:X}", foo) -> "0xDEADBEEF"
|
||||
|
@ -126,15 +126,15 @@ fn main() {
|
|||
assert_eq!(format!("__", 27), "0x1b");
|
||||
assert_eq!(format!("__", 27), "0x1B");
|
||||
|
||||
println!("{:x}!", 27); // hex with no prefix => 1b
|
||||
println!("{:x}!", 27); // Hex with no prefix => 1b
|
||||
|
||||
println!("{:#010b}", 27); // pad binary with 0, width = 10, => 0b00011011
|
||||
println!("{:#010b}", 27); // Pad binary with 0, width = 10, => 0b00011011
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
## Capture the environments
|
||||
## Capture the environment
|
||||
9.πππ
|
||||
```rust,editable
|
||||
fn get_person() -> String {
|
||||
|
@ -168,15 +168,15 @@ fn main() {
|
|||
**Example**
|
||||
```rust,editable
|
||||
fn main() {
|
||||
// exponent
|
||||
// Exponent
|
||||
println!("{:2e}", 1000000000); // => 1e9
|
||||
println!("{:2E}", 1000000000); // => 1E9
|
||||
|
||||
// pointer address
|
||||
// Pointer address
|
||||
let v= vec![1, 2, 3];
|
||||
println!("{:p}", v.as_ptr()); // => 0x600002324050
|
||||
|
||||
// escape
|
||||
// Escape
|
||||
println!("Hello {{}}"); // => Hello {}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# println! and format!
|
||||
Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
|
||||
some of which include:
|
||||
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).
|
||||
|
@ -8,8 +8,7 @@ some of which include:
|
|||
* `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.
|
||||
All parse text in the same fashion. As a plus, Rust checks format correctness at compile time.
|
||||
|
||||
## `format!`
|
||||
1.π
|
||||
|
|
Loadingβ¦
Reference in New Issue