handle errors

This commit is contained in:
LuKe Tidd 2022-12-13 09:09:30 -05:00
parent ddd68240e7
commit 055e0d69ae
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F

View File

@ -14,10 +14,10 @@ fn main() {
// vars are immutable by default, change this
// The :: syntax in the ::new line indicates that new is an _associated function_ of the String
// a function thats implemented on a type
let mut guess = String::new();
// could still call this without importing it via std::io::stdin
// this can be one line, it is broken up for readability.
loop {
let mut guess = String::new();
io::stdin()
// & indicates that this argument is a reference,
// references are also immutable by default so &mut is needed
@ -29,7 +29,15 @@ fn main() {
// Result's variants are Ok and Err
// shadowing V
let guess: u32 = guess.trim().parse().expect("please type a number for the love of god");
// change from expect which crashes all the time to match
// to perform an action depending on the value of the enum
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("that aint no number I ever heard of");
continue;
}
};
// trim removes space and \n
// single colon is type annotation
//