summaryrefslogtreecommitdiff
path: root/exercises/error_handling/errors4.rs
diff options
context:
space:
mode:
authorTaylor Yu <tlyu@mit.edu>2021-06-06 20:14:29 -0500
committerTaylor Yu <tlyu@mit.edu>2021-06-06 23:08:54 -0500
commit50ab289da6b9eb19a7486c341b00048c516b88c0 (patch)
tree162c10c44aa5119007c8b7d958d2806a6cb07ed7 /exercises/error_handling/errors4.rs
parenta2f0401c4c4be9ac326714f55fee454db22c0862 (diff)
fix: rename result1 to errors4
Also put it in the ERROR HANDLING section where it probably belongs.
Diffstat (limited to 'exercises/error_handling/errors4.rs')
-rw-r--r--exercises/error_handling/errors4.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs
new file mode 100644
index 0000000..0685c37
--- /dev/null
+++ b/exercises/error_handling/errors4.rs
@@ -0,0 +1,29 @@
+// errors4.rs
+// Make this test pass! Execute `rustlings hint errors4` for hints :)
+
+// I AM NOT DONE
+
+#[derive(PartialEq, Debug)]
+struct PositiveNonzeroInteger(u64);
+
+#[derive(PartialEq, Debug)]
+enum CreationError {
+ Negative,
+ Zero,
+}
+
+impl PositiveNonzeroInteger {
+ fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
+ Ok(PositiveNonzeroInteger(value as u64))
+ }
+}
+
+#[test]
+fn test_creation() {
+ assert!(PositiveNonzeroInteger::new(10).is_ok());
+ assert_eq!(
+ Err(CreationError::Negative),
+ PositiveNonzeroInteger::new(-10)
+ );
+ assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
+}