Skip to content

Instantly share code, notes, and snippets.

@Sinabon2004
Created January 10, 2026 21:23
Show Gist options
  • Select an option

  • Save Sinabon2004/15c33cec879be8a8bdc103fd9edf82af to your computer and use it in GitHub Desktop.

Select an option

Save Sinabon2004/15c33cec879be8a8bdc103fd9edf82af to your computer and use it in GitHub Desktop.
use rand::random_range;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("🦀 There is a guess game! 🦀");
println!("Right now I've generated random number in range (0:100)");
let random_number = random_range(0..100);
let total_attempts = 10;
println!("You have {total_attempts} attempts");
let mut guess = String::new();
let mut attempts = 0;
while attempts != total_attempts {
println!("Enter your guess: ");
io::stdin().read_line(&mut guess).expect("failed");
let parsed_guess: i32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Failed to parse, please type correct number");
guess.clear();
continue;
}
};
attempts+=1;
match parsed_guess.cmp(&random_number) {
Ordering::Less => {
println!("Your number less than generated");
}
Ordering::Greater => {
println!("Your number more than generated");
}
Ordering::Equal => {
println!("You are right, that was {random_number}");
break;
}
}
let attempts_remaining = total_attempts - attempts;
println!("{}/{} attempts remaining", attempts_remaining, total_attempts);
guess.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment