summaryrefslogtreecommitdiff
path: root/exercises/error_handling
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/error_handling')
-rw-r--r--exercises/error_handling/errors1.rs8
-rw-r--r--exercises/error_handling/errors2.rs3
-rw-r--r--exercises/error_handling/errors3.rs2
-rw-r--r--exercises/error_handling/errors4.rs3
-rw-r--r--exercises/error_handling/errors5.rs18
-rw-r--r--exercises/error_handling/errors6.rs6
-rw-r--r--exercises/error_handling/mod.rs6
7 files changed, 28 insertions, 18 deletions
diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs
index c417fb2..bcee972 100644
--- a/exercises/error_handling/errors1.rs
+++ b/exercises/error_handling/errors1.rs
@@ -3,16 +3,16 @@
// you pass it an empty string. It'd be nicer if it explained what the problem
// was, instead of just sometimes returning `None`. Thankfully, Rust has a similar
// construct to `Option` that can be used to express error conditions. Let's use it!
-// Execute `rustlings hint errors1` for hints!
+// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub fn generate_nametag_text(name: String) -> Option<String> {
- if name.len() > 0 {
- Some(format!("Hi! My name is {}", name))
- } else {
+ if name.is_empty() {
// Empty names aren't allowed.
None
+ } else {
+ Some(format!("Hi! My name is {}", name))
}
}
diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs
index aad3a93..1cd8fc6 100644
--- a/exercises/error_handling/errors2.rs
+++ b/exercises/error_handling/errors2.rs
@@ -14,7 +14,8 @@
// and add.
// There are at least two ways to implement this that are both correct-- but
-// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
+// one is a lot shorter!
+// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs
index 460ac5c..a2d2d19 100644
--- a/exercises/error_handling/errors3.rs
+++ b/exercises/error_handling/errors3.rs
@@ -2,7 +2,7 @@
// This is a program that is trying to use a completed version of the
// `total_cost` function from the previous exercise. It's not working though!
// Why not? What should we do to fix it?
-// Execute `rustlings hint errors3` for hints!
+// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs
index 0685c37..0efe8cc 100644
--- a/exercises/error_handling/errors4.rs
+++ b/exercises/error_handling/errors4.rs
@@ -1,5 +1,5 @@
// errors4.rs
-// Make this test pass! Execute `rustlings hint errors4` for hints :)
+// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@@ -14,6 +14,7 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
+ // Hmm...? Why is this only returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
}
}
diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs
index 365a869..67411c5 100644
--- a/exercises/error_handling/errors5.rs
+++ b/exercises/error_handling/errors5.rs
@@ -1,8 +1,18 @@
// errors5.rs
-// This program uses a completed version of the code from errors4.
-// It won't compile right now! Why?
-// Execute `rustlings hint errors5` for hints!
+// 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.
+
+// 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
@@ -11,7 +21,7 @@ use std::fmt;
use std::num::ParseIntError;
// TODO: update the return type of `main()` to make this compile.
-fn main() -> Result<(), ParseIntError> {
+fn main() -> Result<(), Box<dyn ???>> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs
index 847a049..1306fb0 100644
--- a/exercises/error_handling/errors6.rs
+++ b/exercises/error_handling/errors6.rs
@@ -6,7 +6,7 @@
// we define a custom error type to make it possible for callers to decide
// what to do next when our function returns an error.
-// Make these tests pass! Execute `rustlings hint errors6` for hints :)
+// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@@ -20,7 +20,11 @@ enum ParsePosNonzeroError {
}
impl ParsePosNonzeroError {
+ fn from_creation(err: CreationError) -> ParsePosNonzeroError {
+ ParsePosNonzeroError::Creation(err)
+ }
// TODO: add another error conversion function here.
+ // fn from_parseint...
}
fn parse_pos_nonzero(s: &str)
diff --git a/exercises/error_handling/mod.rs b/exercises/error_handling/mod.rs
deleted file mode 100644
index 539fa23..0000000
--- a/exercises/error_handling/mod.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-mod errors1;
-mod errors2;
-mod errors3;
-mod errors4;
-mod errors5;
-mod errors6;