]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
1 | use rand::Rng; |
2 | use std::cmp::Ordering; | |
3 | use std::io; | |
4 | ||
5 | fn main() { | |
6 | println!("Guess the number!"); | |
7 | ||
6a06907d | 8 | let secret_number = rand::thread_rng().gen_range(1..101); |
74b04a01 XL |
9 | |
10 | println!("The secret number is: {}", secret_number); | |
11 | ||
12 | loop { | |
13 | println!("Please input your guess."); | |
14 | ||
15 | let mut guess = String::new(); | |
16 | ||
17 | // ANCHOR: here | |
18 | // --snip-- | |
19 | ||
20 | io::stdin() | |
21 | .read_line(&mut guess) | |
22 | .expect("Failed to read line"); | |
23 | ||
24 | // ANCHOR: ch19 | |
25 | let guess: u32 = match guess.trim().parse() { | |
26 | Ok(num) => num, | |
27 | Err(_) => continue, | |
28 | }; | |
29 | // ANCHOR_END: ch19 | |
30 | ||
31 | println!("You guessed: {}", guess); | |
32 | ||
33 | // --snip-- | |
34 | // ANCHOR_END: here | |
35 | ||
36 | match guess.cmp(&secret_number) { | |
37 | Ordering::Less => println!("Too small!"), | |
38 | Ordering::Greater => println!("Too big!"), | |
39 | Ordering::Equal => { | |
40 | println!("You win!"); | |
41 | break; | |
42 | } | |
43 | } | |
44 | } | |
45 | } |