Skip to content

Instantly share code, notes, and snippets.

@kevincolyer
Created November 28, 2021 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolyer/e0b9f3b37d4097a9c9ebb8472f933e90 to your computer and use it in GitHub Desktop.
Save kevincolyer/e0b9f3b37d4097a9c9ebb8472f933e90 to your computer and use it in GitHub Desktop.
Guess the number game in Rust language
extern crate text_io;
use std::io::stdout;
use std::io::Write;
use text_io::read;
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let num: u8 = rng.gen_range(1..101);
let maxguesses = 7;
println!("I'm thinking of a number from 1 to 100. What number am I thinking of?");
for count in (1..=maxguesses).rev() {
print!("\nYou have {} guesses\nWhat is your guess?\n>", count);
stdout().flush().unwrap();
let input: u8 = read!();
// echo back input
print!("{}: ", input);
if input > num {
println!("Too high!")
} else if input < num {
println!("Too low!")
} else {
println!("That's it! I was thinking of {}!", num);
return;
}
}
println!("You lose! The number was {}", num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment