fix some typos

This commit is contained in:
sunface 2022-03-15 19:42:27 +08:00
parent b62432fe2a
commit affa25fc54
2 changed files with 54 additions and 19 deletions

View File

@ -42,7 +42,7 @@ 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 output: Now 7 will print! */
/* Make it print: Now 7 will print! */
println!("Now {:?} will print!", Deep(Structure(7)));
}
```
@ -69,10 +69,46 @@ impl fmt::Debug for Point2D {
}
fn main() {
let point = Point2D { x: 3.3, y: 7.2 };
println!("{}", point);
println!("{:?}", point);
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }");
println!("Success!")
}
```
5.
```rust
use std::fmt; // Import the `fmt` module.
// Define a structure named `List` containing a `Vec`.
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, "{}: {}",count, 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]");
}
```

View File

@ -67,7 +67,7 @@ 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 output: Now 7 will print! */
/* Make it print: Now 7 will print! */
println!("Now {:?} will print!", Deep(Structure(7)));
}
```
@ -81,6 +81,8 @@ Anotherthing to note: the placefolder for `Display` is `{}` not `{:?}`.
4. 🌟🌟
```rust,editable
/* Make it work*/
use std::fmt;
struct Point2D {
@ -97,31 +99,27 @@ impl fmt::Debug for Point2D {
}
fn main() {
let point = Point2D { x: 3.3, y: 7.2 };
/* Make it output:
Display: 3.3 + 7.2i
*/
println!("{}", point);
/* Make it output:
Debug: Complex { real: 3.3, imag: 7.2 }
*/
println!("{:?}", point);
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }");
println!("Success!")
}
```
### Example
### `?` 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
use std::fmt; // Import the `fmt` module.
// Define a structure named `List` containing a `Vec`.
/* Make it work */
use std::fmt;
struct List(Vec<i32>);
impl fmt::Display for List {
@ -148,7 +146,8 @@ impl fmt::Display for List {
fn main() {
let v = List(vec![1, 2, 3]);
println!("{}", v);
assert_eq!(format!("{}",v), "[0: 1, 1: 2, 2: 3]");
println!("Success!")
}
```