I am having a bit of trouble with errors2.rs in rustlings. It's a bit annoying because it feels like I've had a bit of a mental block on how to do this correctly.
I tried unwrap and expect however they didn't seem to match what the rustlings test wanted. Can someone share a little wisdom on where I've gone wrong with this?
Since you tried unwrap and expect I'm guessing you understand that here:
let qty = item_quantity.parse::<i32>();
qty is a Result and you need to get the i32 out from inside of it. However, the other key part of the exercise is
and in [the case of an error], we want to immediately return that error from our function
unwrap and expect panic instead of returning, i.e. propagating, the error. (Panicking is not considered a form of returning in Rust, like one might argue exceptions are in other languages, as the panic may directly cause an abort (among other reasons).) They want you to figure out how to return the error.
[spoilers ahead]
Following that chapter, you could match on the returned value:
let qty = match item_quantity.parse::<i32>() {
Ok(q) => q,
Err(e) => return Err(e),
};