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);//生成随机数字
loop {
println!("Please input your guess.");
let mut guess = String::new(); // 声明可变量
//标准库引用 读取一行
io::stdin().
read_line(&mut guess).
expect("Failed to read line ");//expect 会导致程序崩溃,并显示当做参数传递给 expect 的信息。
let guess: u32 = guess.trim().parse().expect("Please type a number!");
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) { // match 分支比较
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}