Error handling is tedious

This commit is contained in:
Justin Kelz 2022-04-03 18:16:24 -07:00
parent d279e7e191
commit 14826e6232
2 changed files with 14 additions and 3 deletions

View File

@ -4,7 +4,6 @@
// Why not? What should we do to fix it?
// Execute `rustlings hint errors3` for hints!
// I AM NOT DONE
use std::num::ParseIntError;

View File

@ -1,7 +1,6 @@
// errors4.rs
// Make this test pass! Execute `rustlings hint errors4` for hints :)
// I AM NOT DONE
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@ -14,9 +13,22 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
/*
The following works, but I think could be best solved with a match case, or enum?
*/
if value > 0 {
Ok(PositiveNonzeroInteger(value as u64))
}
else if
value < 0 {
Err(CreationError::Negative)
}
else {
Err(CreationError::Zero)
}
}
}
#[test]
fn test_creation() {