extern crate rand;

use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    print!("secret_number is {}\n", secret_number);

    loop {
        // 为什么必须在loop里面做new
        let mut guess = String::new();
        println!("Please input your number:");
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");


        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        println!("your guess : {}", guess);
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("less"),
            Ordering::Equal => {
                println!("you win");
                break;
            }
            Ordering::Greater => println!("big"),
        }
    }
}