mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-24 11:13:48 +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::fs;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use std::error;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Error, PartialEq)]
|
#[derive(Clone, Debug, Eq, Error, PartialEq)]
|
||||||
#[error("Found no username in {0}")]
|
#[error("Found no username in {0}")]
|
||||||
struct EmptyUsernameError(String);
|
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();
|
let mut username = String::new();
|
||||||
fs::File::open(path)?.read_to_string(&mut username)?;
|
fs::File::open(path)?.read_to_string(&mut username)?;
|
||||||
if username.is_empty() {
|
if username.is_empty() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user