Rust guessing game test
⚓ Rust 📅 2025-09-15 👤 surdeus 👁️ 13Guessing game using @Pjdur's input function from here.
use rand::Rng;
use std::io::{self, Write};
fn main() {
let player_input: String = input("Guess my number!: ");
let random_number: i32 = rand::rng().random_range(1..=10);
let player_input_new: i32 = player_input.parse().unwrap_or(-1);
let is_it_the_same: bool = player_input_new == random_number;
let the_string: &'static str;
if is_it_the_same {
the_string = "You"
} else {
the_string = "I"
}
if player_input_new == -1 || player_input_new > 10 || player_input_new < 0 {
println!("Pick a actual number between 0 and 10");
} else {
print!(
"Your guess was {}. My guess was {}. {} win!",
player_input_new, random_number, the_string
);
}
}
pub fn input<T: std::fmt::Display>(prompt: T) -> String {
print!("{}", prompt);
io::stdout().flush().unwrap();
let mut line: String = String::new();
io::stdin().read_line(&mut line).unwrap();
line.trim().to_string()
}
2 posts - 2 participants
🏷️ Rust_feed