2023-11-29 10:39:24 -05:00
|
|
|
---
|
|
|
|
minutes: 5
|
|
|
|
---
|
2022-12-21 16:36:30 +01:00
|
|
|
|
2023-11-29 10:39:24 -05:00
|
|
|
# Try Operator
|
|
|
|
|
|
|
|
Runtime errors like connection-refused or file-not-found are handled with the
|
2023-12-31 00:15:07 +01:00
|
|
|
`Result` type, but matching this type on every call can be cumbersome. The
|
2023-11-29 10:39:24 -05:00
|
|
|
try-operator `?` is used to return errors to the caller. It lets you turn the
|
|
|
|
common
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
```rust,ignore
|
|
|
|
match some_expression {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
into the much simpler
|
|
|
|
|
|
|
|
```rust,ignore
|
|
|
|
some_expression?
|
|
|
|
```
|
|
|
|
|
2023-07-06 16:25:37 +02:00
|
|
|
We can use this to simplify our error handling code:
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
```rust,editable
|
2023-06-22 16:27:06 +02:00
|
|
|
use std::io::Read;
|
2023-12-31 00:15:07 +01:00
|
|
|
use std::{fs, io};
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
fn read_username(path: &str) -> Result<String, io::Error> {
|
|
|
|
let username_file_result = fs::File::open(path);
|
|
|
|
let mut username_file = match username_file_result {
|
|
|
|
Ok(file) => file,
|
2023-04-27 10:10:49 -07:00
|
|
|
Err(err) => return Err(err),
|
2022-12-21 16:36:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut username = String::new();
|
|
|
|
match username_file.read_to_string(&mut username) {
|
|
|
|
Ok(_) => Ok(username),
|
2023-04-27 10:10:49 -07:00
|
|
|
Err(err) => Err(err),
|
2022-12-21 16:36:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
//fs::write("config.dat", "alice").unwrap();
|
|
|
|
let username = read_username("config.dat");
|
2023-01-06 12:35:05 -08:00
|
|
|
println!("username or error: {username:?}");
|
2022-12-21 16:36:30 +01:00
|
|
|
}
|
|
|
|
```
|
2023-01-11 18:07:14 -08:00
|
|
|
|
|
|
|
<details>
|
|
|
|
|
2023-11-29 10:39:24 -05:00
|
|
|
Simplify the `read_username` function to use `?`.
|
|
|
|
|
2023-01-11 18:07:14 -08:00
|
|
|
Key points:
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- The `username` variable can be either `Ok(string)` or `Err(error)`.
|
|
|
|
- Use the `fs::write` call to test out the different scenarios: no file, empty
|
|
|
|
file, file with username.
|
|
|
|
- Note that `main` can return a `Result<(), E>` as long as it implements
|
|
|
|
`std::process:Termination`. In practice, this means that `E` implements
|
|
|
|
`Debug`. The executable will print the `Err` variant and return a nonzero exit
|
|
|
|
status on error.
|
2023-01-11 18:07:14 -08:00
|
|
|
|
|
|
|
</details>
|