猜数字游戏
extern crate rand; use std::io; use std::cmp::Ordering; use rand::Rng; fn main() { println!("==猜数字游戏=="); let secret_number = rand::thread_rng().gen_range(1, 101); //println!("产生的加密数字为: {}", secret_number); loop { println!("请输入你的数字: "); let mut guess = String::new(); io::stdin().read_line(&mut guess).expect("输入错误"); if guess.trim().eq("quit") { println!("退出游戏"); return; } println!("你输入的数字: {}", guess.trim()); let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => continue, }; println!("输入的数字为: {:?}", guess); match guess.cmp(&secret_number) { Ordering::Less => println!("数字太小"), Ordering::Greater => println!("数字太大"), Ordering::Equal => { println!("你猜对了"); break; }, } } }