mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 23:04:17 -05:00
Merge pull request #73 from hynek/master
Fix error_handling examples to use the ? operator
This commit is contained in:
commit
2fa0fa17f9
4 changed files with 21 additions and 26 deletions
File diff suppressed because one or more lines are too long
|
@ -66,7 +66,7 @@ mod tests {
|
||||||
// One way to handle this is using a `match` statement on
|
// One way to handle this is using a `match` statement on
|
||||||
// `item_quantity.parse::<i32>()` where the cases are `Ok(something)` and
|
// `item_quantity.parse::<i32>()` where the cases are `Ok(something)` and
|
||||||
// `Err(something)`. This pattern is very common in Rust, though, so there's
|
// `Err(something)`. This pattern is very common in Rust, though, so there's
|
||||||
// a `try!` macro that does pretty much what you would make that match statement
|
// 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:
|
// do for you! Take a look at this section of the Error Handling chapter:
|
||||||
// https://doc.rust-lang.org/stable/book/error-handling.html#the-try-macro
|
// https://doc.rust-lang.org/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-
|
||||||
// and give it a `try!`
|
// and give it a try!
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// errors3.rs
|
// errors3.rs
|
||||||
// This is a program that is trying to use a completed version of the
|
// 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--
|
// `total_cost` function from the previous exercise. It's not working though--
|
||||||
// we can't call the `try!` macro in the `main()` function! Why not?
|
// we can't use the `?` operator in the `main()` function! Why not?
|
||||||
// What should we do instead? Scroll for hints!
|
// What should we do instead? Scroll for hints!
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
@ -10,7 +10,7 @@ fn main() {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
let cost = try!(total_cost(pretend_user_input));
|
let cost = total_cost(pretend_user_input)?;
|
||||||
|
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
|
@ -23,7 +23,7 @@ fn main() {
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
let qty = try!(item_quantity.parse::<i32>());
|
let qty = item_quantity.parse::<i32>()?;
|
||||||
|
|
||||||
Ok(qty * cost_per_item + processing_fee)
|
Ok(qty * cost_per_item + processing_fee)
|
||||||
}
|
}
|
||||||
|
@ -45,23 +45,18 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Since the `try!` macro returns an `Err` early if the thing it's trying to
|
// Since the `?` operator returns an `Err` early if the thing it's trying to
|
||||||
// do fails, you can only use the `try!` macro in functions that have a
|
// do fails, you can only use the `?` operator in functions that have a
|
||||||
// `Result` as their return type.
|
// `Result` as their return type.
|
||||||
|
|
||||||
// The error that you get if you run this code is:
|
// Hence the error that you get if you run this code is:
|
||||||
|
|
||||||
// ```
|
// ```
|
||||||
// error: mismatched types:
|
// error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
|
||||||
// expected `()`,
|
|
||||||
// found `std::result::Result<_, _>`
|
|
||||||
// ```
|
// ```
|
||||||
|
|
||||||
// which is saying that the expected return type of the `main` function is
|
// So we have to use another way of handling a `Result` within `main`.
|
||||||
// the empty tuple, but we tried to return a `Result`-- and that's happening
|
|
||||||
// in the implementation of `try!`. The `main` function never has a return type,
|
|
||||||
// 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
|
// 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 calling the `try!`
|
// not parse to an integer, and implement that instead of using the `?`
|
||||||
// macro.
|
// operator.
|
||||||
|
|
|
@ -115,21 +115,21 @@ impl error::Error for CreationError {
|
||||||
|
|
||||||
// Next hint: There are three places in `read_and_validate` that we call a
|
// 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).
|
// function that returns a `Result` (that is, the functions might fail).
|
||||||
// Wrap those calls in a `try!` macro call so that we return immediately from
|
// Apply the `?` operator on those calls so that we return immediately from
|
||||||
// `read_and_validate` if those function calls fail.
|
// `read_and_validate` if those function calls fail.
|
||||||
|
|
||||||
// Another hint: under the hood, the `try!` macro calls `From::from`
|
// 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>,
|
// 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
|
// 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
|
// can be returned from the same function because all errors act the same
|
||||||
// since they all implement the `error::Error` trait.
|
// since they all implement the `error::Error` trait.
|
||||||
// Check out this section of the book:
|
// Check out this section of the book:
|
||||||
// https://doc.rust-lang.org/stable/book/error-handling.html#standard-library-traits-used-for-error-handling
|
// https://doc.rust-lang.org/stable/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-
|
||||||
|
|
||||||
// Another another hint: Note that because the `try!` macro returns
|
// Another another hint: Note that because the `?` operator returns
|
||||||
// the *unwrapped* value in the `Ok` case, if we want to return a `Result` from
|
// 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
|
// `read_and_validate` for *its* success case, we'll have to rewrap a value
|
||||||
// that we got from the return value of a `try!` call in an `Ok`-- this will
|
// that we got from the return value of a `?`ed call in an `Ok`-- this will
|
||||||
// look like `Ok(something)`.
|
// look like `Ok(something)`.
|
||||||
|
|
||||||
// Another another another hint: `Result`s must be "used", that is, you'll
|
// Another another another hint: `Result`s must be "used", that is, you'll
|
||||||
|
|
Loading…
Reference in a new issue