finish guessing game
This commit is contained in:
parent
a12011a4a6
commit
ee6e59acf9
@ -7,33 +7,40 @@ fn main() {
|
||||
|
||||
// I remember looking this up. Will find the undeprecated calls later.
|
||||
let secret_number = rand::thread_rng().gen_range(1..=100);
|
||||
println!("The secret number is: {}", secret_number);
|
||||
|
||||
println!("Please input your guess: ");
|
||||
|
||||
// ::new() is an associated type. Function is implemented on this type
|
||||
// Returns a new instance of String
|
||||
let mut guess = String::new();
|
||||
// println!("Secret number: {}", secret_number);
|
||||
|
||||
let apples = 5; // immutable
|
||||
let mut bananas = 7; // mutable
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Failed to read line!"); // Use this if truly want to error out. Use pattern matching otherwise
|
||||
|
||||
// Inner programming me screaming rn. This actually isn't all that
|
||||
// bad tho. What's wrong with reusing variable names?
|
||||
// Rust calls it shadowing.
|
||||
let guess: u32 = guess.trim().parse().expect("Please type a number!");
|
||||
loop {
|
||||
// ::new() is an associated type. Function is implemented on this type
|
||||
// Returns a new instance of String
|
||||
let mut guess = String::new();
|
||||
|
||||
println!("You guessed: {}", guess);
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Failed to read line!"); // Use this if truly want to error out. Use pattern matching otherwise
|
||||
println!("You guessed: {}", guess);
|
||||
|
||||
match guess.cmp(&secret_number) {
|
||||
// pogger matching
|
||||
Ordering::Less => println!("Too Small!"),
|
||||
// -1 => println!("Too Small!"), // Not doable
|
||||
Ordering::Greater => println!("Too big!"),
|
||||
Ordering::Equal => println!("You win!"),
|
||||
// Just like Haskell. This is awesome.
|
||||
// Really just like Haskell, this is sick af.
|
||||
let guess: u32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
match guess.cmp(&secret_number) {
|
||||
// pogger matching
|
||||
Ordering::Less => println!("Too Small!"),
|
||||
// -1 => println!("Too Small!"), // Not doable
|
||||
Ordering::Greater => println!("Too big!"),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user