forked from instrumentisto/rust-incubator
-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.rs
48 lines (40 loc) · 1.08 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
use std::{cmp::Ordering, env, io};
fn main() {
println!("Guess the number!");
let secret_number = get_secret_number();
loop {
println!("Please input your guess.");
let guess = match get_guess_number() {
Some(n) => n,
_ => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
fn get_secret_number() -> u32 {
let secret_number = env::args()
.skip(1)
.take(1)
.last()
.expect("No secret number is specified");
secret_number
.trim()
.parse()
.ok()
.expect("Secret number is not a number")
}
fn get_guess_number() -> Option<u32> {
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
guess.trim().parse().ok()
}