Skip to content

Instantly share code, notes, and snippets.

@vbauerster
Created June 8, 2019 20:00
Show Gist options
  • Save vbauerster/73a85bace55bef06b9cbca4535af646c to your computer and use it in GitHub Desktop.
Save vbauerster/73a85bace55bef06b9cbca4535af646c to your computer and use it in GitHub Desktop.
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