@@ -1,11 +1,11 @@
|
||||
# Unwrapping
|
||||
|
||||
`Ticket::new` now returns a `Result` instead of panicking on invalid inputs.
|
||||
`Ticket::new` now returns a `Result` instead of panicking on invalid inputs.\
|
||||
What does this mean for the caller?
|
||||
|
||||
## Failures can't be (implicitly) ignored
|
||||
|
||||
Unlike exceptions, Rust's `Result` forces you to **handle errors at the call site**.
|
||||
Unlike exceptions, Rust's `Result` forces you to **handle errors at the call site**.\
|
||||
If you call a function that returns a `Result`, Rust won't allow you to implicitly ignore the error case.
|
||||
|
||||
```rust
|
||||
@@ -30,7 +30,7 @@ When you call a function that returns a `Result`, you have two key options:
|
||||
let number = parse_int("42").unwrap();
|
||||
// `expect` lets you specify a custom panic message.
|
||||
let number = parse_int("42").expect("Failed to parse integer");
|
||||
```
|
||||
```
|
||||
- Destructure the `Result` using a `match` expression to deal with the error case explicitly.
|
||||
```rust
|
||||
match parse_int("42") {
|
||||
@@ -41,4 +41,4 @@ When you call a function that returns a `Result`, you have two key options:
|
||||
|
||||
## References
|
||||
|
||||
- The exercise for this section is located in `exercises/05_ticket_v2/07_unwrap`
|
||||
- The exercise for this section is located in `exercises/05_ticket_v2/07_unwrap`
|
||||
|
||||
Reference in New Issue
Block a user