mirror of
https://github.com/notohh/rustlings.git
synced 2024-10-30 08:49:12 -04:00
errors4 solution
This commit is contained in:
parent
c46d8bdf95
commit
9b7a5c041e
3 changed files with 58 additions and 16 deletions
|
@ -1,16 +1,16 @@
|
|||
#[derive(PartialEq, Debug)]
|
||||
struct PositiveNonzeroInteger(u64);
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum CreationError {
|
||||
Negative,
|
||||
Zero,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct PositiveNonzeroInteger(u64);
|
||||
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||
// Hmm... Why is this always returning an Ok value?
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
fn new(value: i64) -> Result<Self, CreationError> {
|
||||
// TODO: This function shouldn't always return an `Ok`.
|
||||
Ok(Self(value as u64))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,11 +24,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_creation() {
|
||||
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
||||
assert_eq!(
|
||||
Err(CreationError::Negative),
|
||||
PositiveNonzeroInteger::new(-10)
|
||||
PositiveNonzeroInteger::new(10),
|
||||
Ok(PositiveNonzeroInteger(10)),
|
||||
);
|
||||
assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
|
||||
assert_eq!(
|
||||
PositiveNonzeroInteger::new(-10),
|
||||
Err(CreationError::Negative),
|
||||
);
|
||||
assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -683,11 +683,9 @@ name = "errors4"
|
|||
dir = "13_error_handling"
|
||||
hint = """
|
||||
`PositiveNonzeroInteger::new` is always creating a new instance and returning
|
||||
an `Ok` result.
|
||||
|
||||
It should be doing some checking, returning an `Err` result if those checks
|
||||
fail, and only returning an `Ok` result if those checks determine that
|
||||
everything is... okay :)"""
|
||||
an `Ok` result. But it should be doing some checking, returning an `Err` if
|
||||
those checks fail, and only returning an `Ok` if those checks determine that
|
||||
everything is… okay :)"""
|
||||
|
||||
[[exercises]]
|
||||
name = "errors5"
|
||||
|
|
|
@ -1 +1,42 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum CreationError {
|
||||
Negative,
|
||||
Zero,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct PositiveNonzeroInteger(u64);
|
||||
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<Self, CreationError> {
|
||||
if value == 0 {
|
||||
Err(CreationError::Zero)
|
||||
} else if value < 0 {
|
||||
Err(CreationError::Negative)
|
||||
} else {
|
||||
Ok(Self(value as u64))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_creation() {
|
||||
assert_eq!(
|
||||
PositiveNonzeroInteger::new(10),
|
||||
Ok(PositiveNonzeroInteger(10)),
|
||||
);
|
||||
assert_eq!(
|
||||
PositiveNonzeroInteger::new(-10),
|
||||
Err(CreationError::Negative),
|
||||
);
|
||||
assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue