Finishing up guessing game

This commit is contained in:
Dan Buch 2016-08-31 08:00:15 -04:00
parent 0cbb22dad6
commit 23574cafae
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
3 changed files with 44 additions and 5 deletions

View File

@ -1,4 +1,20 @@
[root]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Dan Buch <daniel.buch@gmail.com>"]
[dependencies]
rand = "0.3.0"

View File

@ -1,14 +1,36 @@
extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let secret_number = rand::thread_rng().gen_range(1, 101);
let mut guess = String::new();
loop {
println!("Please input your guess.");
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
let mut guess = String::new();
println!("You guessed: {}", guess);
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
},
}
}
}