-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
48 lines (35 loc) · 1.14 KB
/
main.rs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secrect_number = rand::thread_rng().gen_range(1, 101);
// println!("The secret number is: {}", secrect_number);
loop { // 循环
println!("Please input your guess.");
let mut guess = String::new(); // mutable 可改变的
io::stdin().read_line(&mut guess)
.expect("Fail to read line");
// 转换类型 u32 表示无符号的32位值
/*
let guess: u32 = guess.trim().parse()
.expect("Please type a number!");
*/
// 异常处理
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess); // {}是占位符
// 比较
match guess.cmp(&secrect_number) { // 枚举类型
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}