summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exercises/13_error_handling/errors4.rs21
-rw-r--r--rustlings-macros/info.toml8
-rw-r--r--solutions/13_error_handling/errors4.rs43
3 files changed, 57 insertions, 15 deletions
diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs
index 993d42a..ba01e54 100644
--- a/exercises/13_error_handling/errors4.rs
+++ b/exercises/13_error_handling/errors4.rs
@@ -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!(
+ PositiveNonzeroInteger::new(10),
+ Ok(PositiveNonzeroInteger(10)),
+ );
+ assert_eq!(
+ PositiveNonzeroInteger::new(-10),
Err(CreationError::Negative),
- PositiveNonzeroInteger::new(-10)
);
- assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
+ assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero));
}
}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index 74cb79d..d39044c 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -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"
diff --git a/solutions/13_error_handling/errors4.rs b/solutions/13_error_handling/errors4.rs
index 4e18198..c43f493 100644
--- a/solutions/13_error_handling/errors4.rs
+++ b/solutions/13_error_handling/errors4.rs
@@ -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));
+ }
+}