trpl-zh-cn/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs

29 lines
580 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
// ANCHOR: all
use std::io;
// ANCHOR: ch07-04
use rand::Rng;
fn main() {
// ANCHOR_END: ch07-04
println!("Guess the number!");
// ANCHOR: ch07-04
2022-07-03 23:41:32 +08:00
let secret_number = rand::thread_rng().gen_range(1..=100);
2022-02-06 16:43:51 +08:00
// ANCHOR_END: ch07-04
2022-07-03 23:41:32 +08:00
println!("The secret number is: {secret_number}");
2022-02-06 16:43:51 +08:00
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
2022-07-03 23:41:32 +08:00
println!("You guessed: {guess}");
2022-02-06 16:43:51 +08:00
// ANCHOR: ch07-04
}
// ANCHOR_END: ch07-04
// ANCHOR_END: all