Rate and Critique my guess the number game!

Hey guys! This is my first time using the rust language, after hearing about it a lot. I've never really used a statically typed language that yells at you this insanely loudly for even the smallest error regarding mutability and/or memory management.

So long story short, after looking up the basics and many reading the docs sessions later, I got myself a simple guess the number game that I'd like for you all to review, and tell me where I could improve it or just quick tips and tricks and the like.

here it is: HooptyDaDoopty/guess_the_number (github.com)

let usr_num = usr_num.trim().parse::<u8>().expect("Failed to parse");

Since this is user input you shouldn't expect it to be a valid number. It could be typed wrong, it could be larger than 255 or negative.

But using .expect() your program will appear to crash (panic). Instead match on the result to get a valid number or loop again after prompting the user that their input wasn't valid.

Your first expect is fine because it is reasonable to expect the terminal io to function correctly, even if it isn't guaranteed.

1 Like

Yeah, that seems a lot more reasonable to do so! It'd probably be really frustrating to mistype a letter, just before getting the number, and instead of simply asking again, it "crashes". Thanks a lot for that insight!

Same here: guess_the_number/main.rs at ec5223bb6f7ee6aec42a676bdae83bce961cb0b3 · HooptyDaDoopty/guess_the_number · GitHub

I think these continue statements are unnecessary. Did you run cargo clippy on your project?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.