Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Set your secret (1-100):");
let mut secret = String::new();
io::stdin()
.read_line(&mut secret)
.expect("Failed to read line");
let secret: u32 = secret.trim().parse().expect("number please!");
let mut guess = rand::thread_rng().gen_range(1, 101);
let mut too_low: u32 = 0;
let mut too_high: u32 = 101;
loop {
println!("Computer: I guess {}", guess);
match guess.cmp(&secret) {
Ordering::Less => {
println!("You: too low!");
too_low = guess;
guess = rand::thread_rng().gen_range(too_low+1, too_high);
},
Ordering::Greater => {
println!("You: too high!");
too_high = guess;
guess = rand::thread_rng().gen_range(too_low+1, too_high);
},
Ordering::Equal => {
println!("You: you win!");
break;
},
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.