Skip to content

Instantly share code, notes, and snippets.

@zvodd
Created May 9, 2022 16:28
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 zvodd/5f8298a470d91b37cdf2b6c284c8867f to your computer and use it in GitHub Desktop.
Save zvodd/5f8298a470d91b37cdf2b6c284c8867f to your computer and use it in GitHub Desktop.
Baby's first rust code. A basic guessing game that was primarily a product of trail and error, as tutorials just aren't as much fun as breaking things.
use rand::Rng;
use std::io;
static HINT_HIGHER: &str = "higher";
static HINT_LOWER: &str = "lower";
fn main() {
println!("Guess the integer between 0 and 9.");
let target: u8 = rand::thread_rng().gen_range(1..10);
let mut attempts_dec: u8 = 5;
loop {
if attempts_dec <= 0 {
println!("Too many attempts, tedium is my end.");
break;
}
let mut guess = String::new();
println!("Enter A Guess ({} attempts left)", attempts_dec);
io::stdin()
.read_line(&mut guess)
.expect("failed to read line");
let guessint: u8;
match guess.trim_end().parse::<u8>() {
Ok(n) => guessint = n,
_ => {
println!("What the heck is \"{}\"?!", guess);
continue;
}
}
attempts_dec -= 1;
let mut hint = HINT_LOWER;
if guessint != target {
if guessint < target {
hint = HINT_HIGHER;
}
println!("You guessed: {}\n Try a {} number.\n", guessint, hint);
continue;
} else {
println!(
" Congrats, you correctly guesssed {}.\nI can die complete now.",
guessint
);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment