Formatter
This commit is contained in:
@@ -13,4 +13,3 @@ We'll need to introduce a few more concepts along the way:
|
||||
- The `Error` trait, to mark error types
|
||||
- The `TryFrom` and `TryInto` traits, for fallible conversions
|
||||
- Rust's package system, explaining what's a library, what's a binary, how to use third-party crates
|
||||
|
||||
|
||||
@@ -41,4 +41,3 @@ enum Status {
|
||||
```
|
||||
|
||||
`enum`, just like `struct`, defines **a new Rust type**.
|
||||
|
||||
|
||||
@@ -68,4 +68,3 @@ match status {
|
||||
```
|
||||
|
||||
The `_` pattern matches anything that wasn't matched by the previous patterns.
|
||||
|
||||
|
||||
@@ -86,4 +86,3 @@ match status {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -64,4 +64,3 @@ as the code that precedes it.
|
||||
Both `if let` and `let/else` are idiomatic Rust constructs.\
|
||||
Use them as you see fit to improve the readability of your code,
|
||||
but don't overdo it: `match` is always there when you need it.
|
||||
|
||||
|
||||
@@ -72,4 +72,3 @@ assert_eq!(second.2, 8);
|
||||
```
|
||||
|
||||
Tuples are a convenient way of grouping values together when you can't be bothered to define a dedicated struct type.
|
||||
|
||||
|
||||
@@ -81,4 +81,3 @@ That's the big advantage of `Result`: it makes fallibility explicit.
|
||||
|
||||
Keep in mind, though, that panics exist. They aren't tracked by the type system, just like exceptions in other languages.
|
||||
But they're meant for **unrecoverable errors** and should be used sparingly.
|
||||
|
||||
|
||||
@@ -38,4 +38,3 @@ When you call a function that returns a `Result`, you have two key options:
|
||||
Err(err) => eprintln!("Error: {}", err),
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -36,4 +36,3 @@ match s.parse_u32() {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -50,4 +50,3 @@ The difference is in their _purpose_: `Display` returns a representation that's
|
||||
while `Debug` provides a low-level representation that's more suitable to developers and service operators.\
|
||||
That's why `Debug` can be automatically implemented using the `#[derive(Debug)]` attribute, while `Display`
|
||||
**requires** a manual implementation.
|
||||
|
||||
|
||||
@@ -61,4 +61,3 @@ binary crate inside. If you want to create a library crate instead, you can use
|
||||
```bash
|
||||
cargo new my-library --lib
|
||||
```
|
||||
|
||||
|
||||
@@ -52,4 +52,3 @@ static_assertions = "1.1.0"
|
||||
```
|
||||
|
||||
We've been using a few of these throughout the book to shorten our tests.
|
||||
|
||||
|
||||
@@ -39,4 +39,3 @@ In the case of `thiserror`, we have:
|
||||
- `#[derive(thiserror::Error)]`: this is the syntax to derive the `Error` trait for a custom error type, helped by `thiserror`.
|
||||
- `#[error("{0}")]`: this is the syntax to define a `Display` implementation for each variant of the custom error type.
|
||||
`{0}` is replaced by the zero-th field of the variant (`String`, in this case) when the error is displayed.
|
||||
|
||||
|
||||
@@ -38,4 +38,3 @@ being attempted.
|
||||
|
||||
Just like `From` and `Into`, `TryFrom` and `TryInto` are dual traits.\
|
||||
If you implement `TryFrom` for a type, you get `TryInto` for free.
|
||||
|
||||
|
||||
@@ -148,4 +148,3 @@ fn read_file() -> Result<String, std::io::Error> {
|
||||
You can use the `?` operator to shorten your error handling code significantly.\
|
||||
In particular, the `?` operator will automatically convert the error type of the fallible operation into the error type
|
||||
of the function, if a conversion is possible (i.e. if there is a suitable `From` implementation)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user