Skip to content

Commit ce18d75

Browse files
committed
session 8 part 1
1 parent 491a318 commit ce18d75

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

errors/src/main.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
fn main() {}
1+
#[derive(Debug)]
2+
enum OutOfBoundsError {
3+
Overflow,
4+
Underflow,
5+
}
6+
7+
struct Adder<'a>(&'a i8);
8+
9+
impl<'a> Adder<'a> {
10+
fn add_one(&self) -> Result<i8, OutOfBoundsError> {
11+
self.0.checked_add(1).ok_or(OutOfBoundsError::Overflow)
12+
}
13+
14+
fn sub_one(&self) -> Result<i8, OutOfBoundsError> {
15+
self.0.checked_sub(1).ok_or(OutOfBoundsError::Underflow)
16+
}
17+
}
18+
19+
fn main() -> Result<(), ()> {
20+
match Adder(&126).add_one() {
21+
Ok(v) => {}
22+
Err(v) => return,
23+
}
24+
Adder(&-128).sub_one().unwrap();
25+
Ok(())
26+
}

0 commit comments

Comments
 (0)