From c393f0e6e5877dc8f45cb865c56bfd07ef481f55 Mon Sep 17 00:00:00 2001 From: Justin Kelz Date: Fri, 1 Apr 2022 21:47:06 -0700 Subject: [PATCH] Finally figured out ? --- exercises/error_handling/errors2.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index aad3a93f..69866b32 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -16,16 +16,20 @@ // 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. -// I AM NOT DONE use std::num::ParseIntError; pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; - let qty = item_quantity.parse::(); - - Ok(qty * cost_per_item + processing_fee) + let qty = item_quantity.parse::()/*?*/; + // The slick answer is just skipping + // the match statement and putting a ? where I left it in the comments. + // It does the same thing as the match statement below. + match qty { + Ok(qty) => Ok(qty * cost_per_item + processing_fee), + Err(ParseIntError) => Err(ParseIntError), + } } #[cfg(test)]