mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-23 19:00:13 +02:00
As done in Rust book
https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html
This commit is contained in:
parent
0ac245ee61
commit
7e59a106a1
@ -7,12 +7,13 @@ all the different possibilities. `std::error::Error` makes this easy.
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
use std::error;
|
||||
|
||||
#[derive(Clone, Debug, Eq, Error, PartialEq)]
|
||||
#[error("Found no username in {0}")]
|
||||
struct EmptyUsernameError(String);
|
||||
|
||||
fn read_username(path: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
fn read_username(path: &str) -> Result<String, Box<dyn error::Error>> {
|
||||
let mut username = String::new();
|
||||
fs::File::open(path)?.read_to_string(&mut username)?;
|
||||
if username.is_empty() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user