2017-12-22 03:27:49 +00:00
|
|
|
extern crate rand;
|
|
|
|
|
2017-12-22 03:49:29 +00:00
|
|
|
use std::cmp::Ordering;
|
2017-12-22 03:25:39 +00:00
|
|
|
use std::io;
|
2017-12-22 03:27:49 +00:00
|
|
|
use rand::Rng;
|
2017-12-22 03:25:39 +00:00
|
|
|
|
2016-08-31 02:49:38 +00:00
|
|
|
fn main() {
|
2017-12-22 03:25:39 +00:00
|
|
|
println!("Guess the number!");
|
|
|
|
|
2017-12-22 03:27:49 +00:00
|
|
|
let secret_number = rand::thread_rng().gen_range(1, 101);
|
|
|
|
|
|
|
|
println!("The secret number is: {}", secret_number);
|
|
|
|
|
2017-12-22 03:25:39 +00:00
|
|
|
println!("Please input your guess.");
|
|
|
|
|
|
|
|
let mut guess = String::new();
|
|
|
|
|
|
|
|
io::stdin().read_line(&mut guess)
|
|
|
|
.expect("Failed to read line");
|
|
|
|
|
2017-12-22 03:49:29 +00:00
|
|
|
let guess: u32 = guess.trim().parse()
|
|
|
|
.expect("Please type a number!");
|
|
|
|
|
2017-12-22 03:25:39 +00:00
|
|
|
println!("You guessed: {}", guess);
|
2017-12-22 03:49:29 +00:00
|
|
|
|
|
|
|
match guess.cmp(&secret_number) {
|
|
|
|
Ordering::Less => println!("Too small!"),
|
|
|
|
Ordering::Greater => println!("Too big!"),
|
|
|
|
Ordering::Equal => println!("You win!"),
|
|
|
|
}
|
2016-08-31 02:49:38 +00:00
|
|
|
}
|