rust number guessing code example
Example: rust guessing game
use std::io;
use rand::Rng;
use std::cmp::Ordering;
fn main() {
let secret:i32 = rand::thread_rng().gen_range(1, 101);
loop{
println!("Ingresa un número: ");
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Failed to read line");
let guess:i32 = match guess.trim().parse(){
Ok(num) => num,
Err(_) => continue,
};
match guess.cmp(&secret){
Ordering::Less => println!("Más arriba!"),
Ordering::Greater => println!("Más abajo!"),
Ordering::Equal => {
println!("Ese mismo compadre");
break;
}
}
}
}