using if and else to capture errors

This commit is contained in:
Hariettemaina 2023-05-22 17:05:01 +03:00
parent 6c2f6772e6
commit fe0cebb8b9

View File

@ -1,7 +1,6 @@
// errors4.rs
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@ -14,11 +13,17 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm...? Why is this only returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
if value > 0 {
Ok(PositiveNonzeroInteger(value as u64))
} else {
if value < 0 {
Err(CreationError::Negative)
} else {
Err(CreationError::Zero)
}
}
}
}
#[test]
fn test_creation() {
assert!(PositiveNonzeroInteger::new(10).is_ok());