diff options
| author | olivia <olivia@fastmail.com> | 2018-11-09 20:31:14 +0100 |
|---|---|---|
| committer | olivia <olivia@fastmail.com> | 2018-11-09 20:31:14 +0100 |
| commit | f7846af7ac388652a6f80a2bbce926ba8f053062 (patch) | |
| tree | 954ee36257047ac612654c5f35e18ed27deda97f /exercises/error_handling | |
| parent | 850a13e9133fedb2fce27884902e0aab94da9692 (diff) | |
right let's try this one again
Diffstat (limited to 'exercises/error_handling')
| -rwxr-xr-x | exercises/error_handling/errors1.rs | 73 | ||||
| -rwxr-xr-x | exercises/error_handling/errors2.rs | 72 | ||||
| -rwxr-xr-x | exercises/error_handling/errors3.rs | 62 | ||||
| -rwxr-xr-x | exercises/error_handling/errorsn.rs | 138 | ||||
| -rwxr-xr-x | exercises/error_handling/option1.rs | 45 | ||||
| -rwxr-xr-x | exercises/error_handling/result1.rs | 43 |
6 files changed, 433 insertions, 0 deletions
diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs new file mode 100755 index 0000000..14ed574 --- /dev/null +++ b/exercises/error_handling/errors1.rs @@ -0,0 +1,73 @@ +// errors1.rs +// This function refuses to generate text to be printed on a nametag if +// you pass it an empty string. It'd be nicer if it explained what the problem +// was, instead of just sometimes returning `None`. The 2nd test currently +// does not compile or pass, but it illustrates the behavior we would like +// this function to have. +// Scroll down for hints!!! + +pub fn generate_nametag_text(name: String) -> Option<String> { + if name.len() > 0 { + Some(format!("Hi! My name is {}", name)) + } else { + // Empty names aren't allowed. + None + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // This test passes initially if you comment out the 2nd test. + // You'll need to update what this test expects when you change + // the function under test! + #[test] + fn generates_nametag_text_for_a_nonempty_name() { + assert_eq!( + generate_nametag_text("Beyoncé".into()), + Some("Hi! My name is Beyoncé".into()) + ); + } + + #[test] + fn explains_why_generating_nametag_text_fails() { + assert_eq!( + generate_nametag_text("".into()), + Err("`name` was empty; it must be nonempty.".into()) + ); + } +} + + + + + + + + + + + + + + + + + + + + +// `Err` is one of the variants of `Result`, so what the 2nd test is saying +// is that `generate_nametag_text` should return a `Result` instead of an +// `Option`. + +// To make this change, you'll need to: +// - update the return type in the function signature to be a Result that +// could be the variants `Ok(String)` and `Err(String)` +// - change the body of the function to return `Ok(stuff)` where it currently +// returns `Some(stuff)` +// - change the body of the function to return `Err(error message)` where it +// currently returns `None` +// - change the first test to expect `Ok(stuff)` where it currently expects +// `Some(stuff)`. diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs new file mode 100755 index 0000000..15c21c8 --- /dev/null +++ b/exercises/error_handling/errors2.rs @@ -0,0 +1,72 @@ +// errors2.rs +// Say we're writing a game where you can buy items with tokens. All items cost +// 5 tokens, and whenever you purchase items there is a processing fee of 1 +// token. A player of the game will type in how many items they want to buy, +// and the `total_cost` function will calculate the total number of tokens. +// Since the player typed in the quantity, though, we get it as a string-- and +// they might have typed anything, not just numbers! + +// Right now, this function isn't handling the error case at all (and isn't +// handling the success case properly either). What we want to do is: +// if we call the `parse` function on a string that is not a number, that +// function will return a `ParseIntError`, and in that case, we want to +// immediately return that error from our function and not try to multiply +// and add. + +// There are at least two ways to implement this that are both correct-- but +// one is a lot shorter! Scroll down for hints to both ways. + +use std::num::ParseIntError; + +pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { + let processing_fee = 1; + let cost_per_item = 5; + let qty = item_quantity.parse::<i32>(); + + Ok(qty * cost_per_item + processing_fee) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn item_quantity_is_a_valid_number() { + assert_eq!( + total_cost("34"), + Ok(171) + ); + } + + #[test] + fn item_quantity_is_an_invalid_number() { + assert_eq!( + total_cost("beep boop").unwrap_err().to_string(), + "invalid digit found in string" + ); + } +} + + + + + + + + + + + + + + + + + +// One way to handle this is using a `match` statement on +// `item_quantity.parse::<i32>()` where the cases are `Ok(something)` and +// `Err(something)`. This pattern is very common in Rust, though, so there's +// a `?` operator that does pretty much what you would make that match statement +// do for you! Take a look at this section of the Error Handling chapter: +// https://doc.rust-lang.org/stable/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator +// and give it a try! diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs new file mode 100755 index 0000000..9c29af5 --- /dev/null +++ b/exercises/error_handling/errors3.rs @@ -0,0 +1,62 @@ +// errors3.rs +// 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-- +// we can't use the `?` operator in the `main()` function! Why not? +// What should we do instead? Scroll for hints! + +use std::num::ParseIntError; + +fn main() { + let mut tokens = 100; + let pretend_user_input = "8"; + + let cost = total_cost(pretend_user_input)?; + + if cost > tokens { + println!("You can't afford that many!"); + } else { + tokens -= cost; + println!("You now have {} tokens.", tokens); + } +} + +pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { + let processing_fee = 1; + let cost_per_item = 5; + let qty = item_quantity.parse::<i32>()?; + + Ok(qty * cost_per_item + processing_fee) +} + + + + + + + + + + + + + + + + + + +// Since the `?` operator returns an `Err` early if the thing it's trying to +// do fails, you can only use the `?` operator in functions that have a +// `Result` as their return type. + +// Hence the error that you get if you run this code is: + +// ``` +// error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`) +// ``` + +// So we have to use another way of handling a `Result` within `main`. + +// Decide what we should do if `pretend_user_input` has a string value that does +// not parse to an integer, and implement that instead of using the `?` +// operator. diff --git a/exercises/error_handling/errorsn.rs b/exercises/error_handling/errorsn.rs new file mode 100755 index 0000000..15c6cd5 --- /dev/null +++ b/exercises/error_handling/errorsn.rs @@ -0,0 +1,138 @@ +// errorsn.rs +// This is a bigger error exercise than the previous ones! +// You can do it! :) +// +// Edit the `read_and_validate` function so that it compiles and +// passes the tests... so many things could go wrong! +// +// - Reading from stdin could produce an io::Error +// - Parsing the input could produce a num::ParseIntError +// - Validating the input could produce a CreationError (defined below) +// +// How can we lump these errors into one general error? That is, what +// type goes where the question marks are, and how do we return +// that type from the body of read_and_validate? +// +// Scroll down for hints :) + +use std::error; +use std::fmt; +use std::io; + +// PositiveNonzeroInteger is a struct defined below the tests. +fn read_and_validate(b: &mut io::BufRead) -> Result<PositiveNonzeroInteger, ???> { + let mut line = String::new(); + b.read_line(&mut line); + let num: i64 = line.trim().parse(); + let answer = PositiveNonzeroInteger::new(num); + answer +} + +// This is a test helper function that turns a &str into a BufReader. +fn test_with_str(s: &str) -> Result<PositiveNonzeroInteger, Box<error::Error>> { + let mut b = io::BufReader::new(s.as_bytes()); + read_and_validate(&mut b) +} + +#[test] +fn test_success() { + let x = test_with_str("42\n"); + assert_eq!(PositiveNonzeroInteger(42), x.unwrap()); +} + +#[test] +fn test_not_num() { + let x = test_with_str("eleven billion\n"); + assert!(x.is_err()); +} + +#[test] +fn test_non_positive() { + let x = test_with_str("-40\n"); + assert!(x.is_err()); +} + +#[test] +fn test_ioerror() { + struct Broken; + impl io::Read for Broken { + fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { + Err(io::Error::new(io::ErrorKind::BrokenPipe, "uh-oh!")) + } + } + let mut b = io::BufReader::new(Broken); + assert!(read_and_validate(&mut b).is_err()); + assert_eq!("uh-oh!", read_and_validate(&mut b).unwrap_err().to_string()); +} + +#[derive(PartialEq,Debug)] +struct PositiveNonzeroInteger(u64); + +impl PositiveNonzeroInteger { + fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> { + if value == 0 { + Err(CreationError::Zero) + } else if value < 0 { + Err(CreationError::Negative) + } else { + Ok(PositiveNonzeroInteger(value as u64)) + } + } +} + +#[test] +fn test_positive_nonzero_integer_creation() { + assert!(PositiveNonzeroInteger::new(10).is_ok()); + assert_eq!(Err(CreationError::Negative), PositiveNonzeroInteger::new(-10)); + assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0)); +} + +#[derive(PartialEq,Debug)] +enum CreationError { + Negative, + Zero, +} + +impl fmt::Display for CreationError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str((self as &error::Error).description()) + } +} + +impl error::Error for CreationError { + fn description(&self) -> &str { + match *self { + CreationError::Negative => "Negative", + CreationError::Zero => "Zero", + } + } +} + +// First hint: To figure out what type should go where the ??? is, take a look +// at the test helper function `test_with_str`, since it returns whatever +// `read_and_validate` returns and`test_with_str` has its signature fully +// specified. + +// Next hint: There are three places in `read_and_validate` that we call a +// function that returns a `Result` (that is, the functions might fail). +// Apply the `?` operator on those calls so that we return immediately from +// `read_and_validate` if those function calls fail. + +// Another hint: under the hood, the `?` operator calls `From::from` +// on the error value to convert it to a boxed trait object, a Box<error::Error>, +// which is polymorphic-- that means that lots of different kinds of errors +// can be returned from the same function because all errors act the same +// since they all implement the `error::Error` trait. +// Check out this section of the book: +// https://doc.rust-lang.org/stable/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator + +// Another another hint: Note that because the `?` operator returns +// the *unwrapped* value in the `Ok` case, if we want to return a `Result` from +// `read_and_validate` for *its* success case, we'll have to rewrap a value +// that we got from the return value of a `?`ed call in an `Ok`-- this will +// look like `Ok(something)`. + +// Another another another hint: `Result`s must be "used", that is, you'll +// get a warning if you don't handle a `Result` that you get in your +// function. Read more about that in the `std::result` module docs: +// https://doc.rust-lang.org/std/result/#results-must-be-used diff --git a/exercises/error_handling/option1.rs b/exercises/error_handling/option1.rs new file mode 100755 index 0000000..9cf0bc9 --- /dev/null +++ b/exercises/error_handling/option1.rs @@ -0,0 +1,45 @@ +// option1.rs +// This example panics because the second time it calls `pop`, the `vec` +// is empty, so `pop` returns `None`, and `unwrap` panics if it's called +// on `None`. Handle this in a more graceful way than calling `unwrap`! +// Scroll down for hints :) + +fn main() { + let mut list = vec![3]; + + let last = list.pop().unwrap(); + println!("The last item in the list is {:?}", last); + + let second_to_last = list.pop().unwrap(); + println!("The second-to-last item in the list is {:?}", second_to_last); +} + + + + + + + + + + + + + + + + + + + + + + + + + +// Try using a `match` statement where the arms are `Some(thing)` and `None`. +// Or set a default value to print out if you get `None` by using the +// function `unwrap_or`. +// Or use an `if let` statement on the result of `pop()` to both destructure +// a `Some` value and only print out something if we have a value! diff --git a/exercises/error_handling/result1.rs b/exercises/error_handling/result1.rs new file mode 100755 index 0000000..851ab45 --- /dev/null +++ b/exercises/error_handling/result1.rs @@ -0,0 +1,43 @@ +// result1.rs +// Make this test pass! Scroll down for hints :) + +#[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)); +} + + + + + + + + + + + + + + + + +// `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 :) |
