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.
|
// I remember looking this up. Will find the undeprecated calls later.
|
||||||
let secret_number = rand::thread_rng().gen_range(1..=100);
|
let secret_number = rand::thread_rng().gen_range(1..=100);
|
||||||
println!("The secret number is: {}", secret_number);
|
// println!("Secret number: {}", 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();
|
|
||||||
|
|
||||||
let apples = 5; // immutable
|
let apples = 5; // immutable
|
||||||
let mut bananas = 7; // mutable
|
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
|
// Inner programming me screaming rn. This actually isn't all that
|
||||||
// bad tho. What's wrong with reusing variable names?
|
// bad tho. What's wrong with reusing variable names?
|
||||||
// Rust calls it shadowing.
|
// 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) {
|
// Just like Haskell. This is awesome.
|
||||||
// pogger matching
|
// Really just like Haskell, this is sick af.
|
||||||
Ordering::Less => println!("Too Small!"),
|
let guess: u32 = match guess.trim().parse() {
|
||||||
// -1 => println!("Too Small!"), // Not doable
|
Ok(num) => num,
|
||||||
Ordering::Greater => println!("Too big!"),
|
Err(_) => continue,
|
||||||
Ordering::Equal => println!("You win!"),
|
};
|
||||||
|
|
||||||
|
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