]> git.proxmox.com Git - rustc.git/blame - src/doc/book/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / doc / book / listings / ch02-guessing-game-tutorial / no-listing-04-looping / src / main.rs
CommitLineData
74b04a01
XL
1use rand::Rng;
2use std::cmp::Ordering;
3use std::io;
4
5fn main() {
6 println!("Guess the number!");
7
6a06907d 8 let secret_number = rand::thread_rng().gen_range(1..101);
74b04a01
XL
9
10 // ANCHOR: here
11 // --snip--
12
13 println!("The secret number is: {}", secret_number);
14
15 loop {
16 println!("Please input your guess.");
17
18 // --snip--
19
20 // ANCHOR_END: here
21
22 let mut guess = String::new();
23
24 io::stdin()
25 .read_line(&mut guess)
26 .expect("Failed to read line");
27
28 let guess: u32 = guess.trim().parse().expect("Please type a number!");
29
30 println!("You guessed: {}", guess);
31
32 // ANCHOR: here
33 match guess.cmp(&secret_number) {
34 Ordering::Less => println!("Too small!"),
35 Ordering::Greater => println!("Too big!"),
36 Ordering::Equal => println!("You win!"),
37 }
38 }
39}
40// ANCHOR_END: here