forked from t4sk/hello-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox_dyn_error.rs
More file actions
75 lines (58 loc) · 1.59 KB
/
box_dyn_error.rs
File metadata and controls
75 lines (58 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![allow(unused)]
// Implementing std::error::Error for MathError and ParseError
#[derive(Debug)]
enum MathError {
DivByZero,
}
#[derive(Debug)]
enum ParseError {
InvalidInt,
}
impl std::fmt::Display for MathError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "math error {:?}", self)
}
}
impl std::error::Error for MathError {}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "parse error {:?}", self)
}
}
impl std::error::Error for ParseError {}
fn f1() -> Result<u32, MathError> {
Err(MathError::DivByZero)
}
fn f2() -> Result<u32, ParseError> {
Err(ParseError::InvalidInt)
}
fn f3() -> Result<(), Box<dyn std::error::Error>> {
f1()?;
f2()?;
Ok(())
}
// Practical example
use std::env;
use std::fs::File;
use std::io::Read;
use std::num::ParseIntError;
fn read(src_path: &str) -> Result<Vec<String>, std::io::Error> {
let mut src_file = File::open(src_path)?;
let mut data = String::new();
src_file.read_to_string(&mut data)?;
let lines: Vec<String> = data.trim().split('\n').map(|s| s.to_string()).collect();
Ok(lines)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
// read returns Result<Vec<String>, std::io::Error>
let lines = read(&args[1])?;
let mut sum: i32 = 0;
for line in lines {
// parse returns Result<i32, ParseIntError>
let num: i32 = line.parse()?;
sum += num;
}
println!("{}", sum);
Ok(())
}