Last active
April 4, 2022 08:27
-
-
Save Integralist/61c77417bc253aac77e853ecbf206cb1 to your computer and use it in GitHub Desktop.
[Rust Book Examples] #rust #rng
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rand::Rng; | |
use std::cmp::Ordering; | |
use std::io; | |
fn main() { | |
let secret_number = rand::thread_rng().gen_range(1, 101); | |
println!("secret number: {}", secret_number); | |
loop { | |
println!("input a guess..."); | |
let mut guess = String::new(); | |
io::stdin() | |
.read_line(&mut guess) | |
.expect("failed to read input"); | |
let guess: u32 = guess.trim().parse().expect("please type a number"); | |
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment