2022-12-21 16:36:30 +01:00
|
|
|
# Propagating Errors with `?`
|
|
|
|
|
|
|
|
The try-operator `?` is used to return errors to the caller. It lets you turn
|
|
|
|
the common
|
|
|
|
|
|
|
|
```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::{fs, io};
|
|
|
|
use std::io::Read;
|
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>
|
|
|
|
|
|
|
|
Key points:
|
|
|
|
|
|
|
|
* 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.
|
2023-07-28 13:48:58 +02:00
|
|
|
* The return type of the function has to be compatible with the nested functions it calls. For instance,
|
|
|
|
a function returning a `Result<T, Err>` can only apply the `?` operator on a function returning a
|
2023-08-09 16:48:15 +02:00
|
|
|
`Result<AnyT, Err>`. It cannot apply the `?` operator on a function returning an `Option<AnyT>` or `Result<T, OtherErr>`
|
|
|
|
unless `OtherErr` implements `From<Err>`. Reciprocally, a function returning an `Option<T>` can only apply the `?` operator
|
2023-07-28 13:48:58 +02:00
|
|
|
on a function returning an `Option<AnyT>`.
|
|
|
|
* You can convert incompatible types into one another with the different `Option` and `Result` methods
|
|
|
|
such as `Option::ok_or`, `Result::ok`, `Result::err`.
|
2023-01-11 18:07:14 -08:00
|
|
|
|
|
|
|
</details>
|