mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-18 16:33:09 +02:00
47 lines
949 B
Markdown
47 lines
949 B
Markdown
|
# 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?
|
||
|
```
|
||
|
|
||
|
We can use this to simplify our error handing code:
|
||
|
|
||
|
```rust,editable
|
||
|
use std::fs;
|
||
|
use std::io::{self, Read};
|
||
|
|
||
|
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,
|
||
|
Err(e) => return Err(e),
|
||
|
};
|
||
|
|
||
|
let mut username = String::new();
|
||
|
|
||
|
match username_file.read_to_string(&mut username) {
|
||
|
Ok(_) => Ok(username),
|
||
|
Err(e) => Err(e),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
//fs::write("config.dat", "alice").unwrap();
|
||
|
let username = read_username("config.dat");
|
||
|
println!("username: {username:?}");
|
||
|
}
|
||
|
```
|