上面两个图 就表明了RUST强大编译器的作用
extern crate rand;
use std::io;
use rand::Rng;
use std::cmp::Ordering;//trait
fn main() {
println!("猜数!");
let secret_num = rand::thread_rng().gen_range(1..101);
// println!("secret_num: {}", secret_num);
println!("猜测一个数");
loop {
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("无法读取行!");
//io::Result:: Ok,Err
//shadow
println!("{}", guess);
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("please enter a number");
continue;
}
};
println!("你猜测的数:{}", guess);
match guess.cmp(&secret_num) {
Ordering::Less => println!("too small"),
Ordering::Greater => println!("too big"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}