0%

rust 学习记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("猜一个数字!");
println!("输出你猜的数字");
let secret_number = rand::thread_rng().gen_range(1, 101);
loop {
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("读取错误");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("请输入数字");
continue;
}
};
match guess.cmp(&secret_number) {
Ordering::Less => println!("小了"),
Ordering::Greater => println!("大了"),
Ordering::Equal => {
println!("对了");
break;
}
}
}
}