this post was submitted on 03 Jun 2021
13 points (100.0% liked)

Rust Programming

8438 readers
42 users here now

founded 5 years ago
MODERATORS
 

Hi, first of all sorry if it's not the place to ask (I'll delete the post if it's the case).

I started to learn Rust a few days ago and I'm currently doing the rustlings course. I'm at the exercise error_handling/errors2.rs. I've succeed to make it compile using the ? operator which I find kinda easy to understand.

My problem is the following: It's stated in the exercise that there's another solution. After looking at the hint, I know that the other solution is the use of match. I've kept looking at the docs, the examples and also StackOverflow posts but I can't make it work since I really don't understand how it's supposed to work.

Here's what I tried:

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
         let processing_fee = 1;
         let cost_per_item = 5;
         let qty = item_quantity.parse();

         match qty {
                 Ok(qty) => qty * cost_per_item + processing_fee,
                 Err(e) => Err(e),                                           
         };
}

And here's what the compiler give:expected enum 'Result<i32, ParseIntError>' found unit type '()'

top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 3 years ago (1 children)

You need to remove the ";" at the end of the match block. That's what causes that unit type (i.e. nothing) to be returned.

I can spot one other error, but I imagine you will be able to figure that out yourself.

[–] [email protected] 1 points 3 years ago* (last edited 3 years ago) (2 children)

Thanks! Now I don't understand the purpose of the semi-colon at the end of the match block in the examples lol.

I guess the other error was to add qty * cost_per_item + processing_fee inside an Ok(). (Or maybe to add ::<i32> after the parse function that I somehow deleted)

[–] [email protected] 6 points 3 years ago (1 children)

If the last statement in a function has no semicolon at the end, it will automatically be returned from the function (just like using the return function). If it has a semicolon, nothing will be returned (which is the unit type () in your error message).

[–] [email protected] 3 points 3 years ago (1 children)

Oh, I get it! So it's that simple lol. I feel stupid :).

[–] [email protected] 7 points 3 years ago* (last edited 3 years ago) (1 children)

Never feel stupid about such things. This is how learning works. And, in my opinion, it is one of the most amazing processes of a human mind. Have fun with Rust.

[–] [email protected] 3 points 3 years ago
[–] [email protected] 4 points 3 years ago

Yeah, needing to wrap that in an Ok() is what I had spotted. :)

[–] [email protected] 4 points 3 years ago* (last edited 3 years ago) (1 children)

You also don't need that match there. You should be able to do :

let qty = item_quantity.parse()?;
let whatever = qty * cost_per_item + processing_fee;
Ok(whatever)

Check out the sections on error handling in the rust book.

[–] [email protected] 2 points 3 years ago

That's what I did at first but I wanted to understand how to use match since they stated that we could use both case.