mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
fix some typos
This commit is contained in:
@ -42,7 +42,7 @@ fn main() {
|
|||||||
// The problem with `derive` is there is no control over how
|
// The problem with `derive` is there is no control over how
|
||||||
// the results look. What if I want this to just show a `7`?
|
// 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)));
|
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -69,10 +69,46 @@ impl fmt::Debug for Point2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let point = Point2D { x: 3.3, y: 7.2 };
|
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]");
|
||||||
}
|
}
|
||||||
```
|
```
|
@ -67,7 +67,7 @@ fn main() {
|
|||||||
// The problem with `derive` is there is no control over how
|
// The problem with `derive` is there is no control over how
|
||||||
// the results look. What if I want this to just show a `7`?
|
// 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)));
|
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -81,6 +81,8 @@ Anotherthing to note: the placefolder for `Display` is `{}` not `{:?}`.
|
|||||||
|
|
||||||
4. 🌟🌟
|
4. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
|
/* Make it work*/
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
struct Point2D {
|
struct Point2D {
|
||||||
@ -97,31 +99,27 @@ impl fmt::Debug for Point2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let point = Point2D { x: 3.3, y: 7.2 };
|
let point = Point2D { x: 3.3, y: 7.2 };
|
||||||
/* Make it output:
|
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
|
||||||
Display: 3.3 + 7.2i
|
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }");
|
||||||
*/
|
|
||||||
println!("{}", point);
|
|
||||||
|
|
||||||
/* Make it output:
|
println!("Success!")
|
||||||
Debug: Complex { real: 3.3, imag: 7.2 }
|
|
||||||
*/
|
|
||||||
println!("{:?}", point);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### 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.
|
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`.
|
Fortunately, Rust provides the `?` operator to help us eliminate some unnecessary codes for deaing with `fmt::Result`.
|
||||||
|
|
||||||
|
5. 🌟🌟
|
||||||
```rust,editable
|
```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>);
|
struct List(Vec<i32>);
|
||||||
|
|
||||||
impl fmt::Display for List {
|
impl fmt::Display for List {
|
||||||
@ -148,7 +146,8 @@ impl fmt::Display for List {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = List(vec![1, 2, 3]);
|
let v = List(vec![1, 2, 3]);
|
||||||
println!("{}", v);
|
assert_eq!(format!("{}",v), "[0: 1, 1: 2, 2: 3]");
|
||||||
|
println!("Success!")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user