summaryrefslogtreecommitdiff
path: root/exercises/13_error_handling/errors5.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/13_error_handling/errors5.rs
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/13_error_handling/errors5.rs')
-rw-r--r--exercises/13_error_handling/errors5.rs83
1 files changed, 34 insertions, 49 deletions
diff --git a/exercises/13_error_handling/errors5.rs b/exercises/13_error_handling/errors5.rs
index 92461a7..d0044db 100644
--- a/exercises/13_error_handling/errors5.rs
+++ b/exercises/13_error_handling/errors5.rs
@@ -1,45 +1,18 @@
-// errors5.rs
-//
-// This program uses an altered version of the code from errors4.
-//
-// This exercise uses some concepts that we won't get to until later in the
-// course, like `Box` and the `From` trait. It's not important to understand
-// them in detail right now, but you can read ahead if you like. For now, think
-// of the `Box<dyn ???>` type as an "I want anything that does ???" type, which,
-// given Rust's usual standards for runtime safety, should strike you as
-// somewhat lenient!
+// This exercise is an altered version of the `errors4` exercise. It uses some
+// concepts that we won't get to until later in the course, like `Box` and the
+// `From` trait. It's not important to understand them in detail right now, but
+// you can read ahead if you like. For now, think of the `Box<dyn ???>` type as
+// an "I want anything that does ???" type.
//
// In short, this particular use case for boxes is for when you want to own a
// value and you care only that it is a type which implements a particular
-// trait. To do so, The Box is declared as of type Box<dyn Trait> where Trait is
-// the trait the compiler looks for on any value used in that context. For this
-// exercise, that context is the potential errors which can be returned in a
-// Result.
-//
-// What can we use to describe both errors? In other words, is there a trait
-// which both errors implement?
-//
-// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
+// trait. To do so, The `Box` is declared as of type `Box<dyn Trait>` where
+// `Trait` is the trait the compiler looks for on any value used in that
+// context. For this exercise, that context is the potential errors which
+// can be returned in a `Result`.
-use std::error;
+use std::error::Error;
use std::fmt;
-use std::num::ParseIntError;
-
-// TODO: update the return type of `main()` to make this compile.
-fn main() -> Result<(), Box<dyn ???>> {
- let pretend_user_input = "42";
- let x: i64 = pretend_user_input.parse()?;
- println!("output={:?}", PositiveNonzeroInteger::new(x)?);
- Ok(())
-}
-
-// Don't change anything below this line.
-
-#[derive(PartialEq, Debug)]
-struct PositiveNonzeroInteger(u64);
#[derive(PartialEq, Debug)]
enum CreationError {
@@ -47,17 +20,7 @@ enum CreationError {
Zero,
}
-impl PositiveNonzeroInteger {
- fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
- match value {
- x if x < 0 => Err(CreationError::Negative),
- x if x == 0 => Err(CreationError::Zero),
- x => Ok(PositiveNonzeroInteger(x as u64)),
- }
- }
-}
-
-// This is required so that `CreationError` can implement `error::Error`.
+// This is required so that `CreationError` can implement `Error`.
impl fmt::Display for CreationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let description = match *self {
@@ -68,4 +31,26 @@ impl fmt::Display for CreationError {
}
}
-impl error::Error for CreationError {}
+impl Error for CreationError {}
+
+#[derive(PartialEq, Debug)]
+struct PositiveNonzeroInteger(u64);
+
+impl PositiveNonzeroInteger {
+ fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
+ match value {
+ 0 => Err(CreationError::Zero),
+ x if x < 0 => Err(CreationError::Negative),
+ x => Ok(PositiveNonzeroInteger(x as u64)),
+ }
+ }
+}
+
+// TODO: Add the correct return type `Result<(), Box<dyn ???>>`. What can we
+// use to describe both errors? Is there a trait which both errors implement?
+fn main() {
+ let pretend_user_input = "42";
+ let x: i64 = pretend_user_input.parse()?;
+ println!("output={:?}", PositiveNonzeroInteger::new(x)?);
+ Ok(())
+}