mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 21:42:23 -05: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)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum CreationError {
|
enum CreationError {
|
||||||
Negative,
|
Negative,
|
||||||
Zero,
|
Zero,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<Self, CreationError> {
|
||||||
// Hmm... Why is this always returning an Ok value?
|
// TODO: This function shouldn't always return an `Ok`.
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Ok(Self(value as u64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,11 +24,14 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_creation() {
|
fn test_creation() {
|
||||||
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
|
||||||
assert_eq!(
|
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"
|
dir = "13_error_handling"
|
||||||
hint = """
|
hint = """
|
||||||
`PositiveNonzeroInteger::new` is always creating a new instance and returning
|
`PositiveNonzeroInteger::new` is always creating a new instance and returning
|
||||||
an `Ok` result.
|
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
|
||||||
It should be doing some checking, returning an `Err` result if those checks
|
everything is… okay :)"""
|
||||||
fail, and only returning an `Ok` result if those checks determine that
|
|
||||||
everything is... okay :)"""
|
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "errors5"
|
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