mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
using if and else to capture errors
This commit is contained in:
parent
6c2f6772e6
commit
fe0cebb8b9
@ -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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user