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]");
}
```