mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19: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
|
// errors4.rs
|
||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
@ -14,11 +13,17 @@ enum CreationError {
|
|||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm...? Why is this only returning an Ok value?
|
if value > 0 {
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
} else {
|
||||||
|
if value < 0 {
|
||||||
|
Err(CreationError::Negative)
|
||||||
|
} else {
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_creation() {
|
fn test_creation() {
|
||||||
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user