mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-05 02:09:18 +00:00
Fix error handling in PositiveNonzeroInteger::new()
This commit is contained in:
parent
dcd82188c3
commit
5a9c46b51e
@ -3,8 +3,6 @@
|
|||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
@ -17,7 +15,13 @@ enum CreationError {
|
|||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm... Why is this always returning an Ok value?
|
// Hmm... Why is this always returning an Ok value?
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
if value == 0 {
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
} else if value < 0 {
|
||||||
|
Err(CreationError::Negative)
|
||||||
|
} else {
|
||||||
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user